You are on page 1of 78

A REPORT ON C++ and C

(For Summer Internship Programme)

For the degree


Bachelor of Technology
In
Computer Science Engineering

Submitted by:
Kashish Aggarwal

Under the guidance of


Mr.Savood Ahmed
(DREAMTECH INFOTECH.)
CERTIFICATE

This is to certify that this report on “C++ and C” submitted by Kashish Aggarwal
from Women institute of technology, Dehradun for the fulfillment of summer
internship for the Degree of Bachelor of Technology in CSE at DREAMTECH
Dehradun is an authentic work carried out by her under my guidance.

To the best of my knowledge, the matter acknowledged in this report has not been
submitted to any other institution for the award of this degree.

Date:
Place:
ACKNOWLEDGEMENT

I would like to express my deep sense of gratitude and respect to my supervisor


Mr.Savood Ahmed, Dreamtech Institute for his excellent guidance, suggestions
and constructive criticism during my internship session. He has not only been a
wonderful supervisor but also a genuine person. I consider myself extremely lucky
to be able to work under the guidance of such a dynamic personality.

I would like to thank him for guiding me under his immense experience and
knowledge during the complete internship session.

Last, but not the least I extend my sincere thanks to Dreamtech for making my
project a successful one, for their valuable advice in every stage and also giving me
absolute working environment. I would like to thank all who directly and indirectly
supported and helped me completing my report on time.

Date:

Place:
Kashish Aggarwal
(CSE, WIT)
ABSTRACT
C++:

The C++ programming language was created by Bjarne Stroustrup and his team at
Bell Laboratories (AT&T, USA) to help implement simulation projects in an
object-oriented and efficient way. The earliest versions, which were originally
referred to as “C with classes,” date back to 1980. As the name C++ implies, C++
was derived from the C programming language: ++ is the increment operator in C.
In 1998 the ISO (International Organization for Standardization) approved a
standard for C++ (ISO/IEC 14882). C++ is not a purely object-oriented language
but a hybrid that contains the functionality of the C programming language. This
means that you have all the features that are available in C:
■ Universally usable modular programs.
■ Efficient, close to the machine programming.
■ Portable programs for various platforms.

C:
The C programming language is a computer programming language that was
developed to do system programming for the operating system UNIX and is
an imperative programming language. C was developed in the early 1970s by Ken
Thompson and Dennis Ritchie at Bell Labs. It is a procedural language, which
means that people can write their programs as a series of step-by-step instructions.
C is a compiled language. Because the ideas behind C are kept close to the design
of the computer, the compiler (program builder) can generate machine code/native
code for the computer. Programs built in machine code are very fast. This makes C
a good language for writing operating systems. Many operating systems,
including Linux and UNIX, are programmed using this language. The language
itself has very few keywords, and most things are done using libraries, which are
collections of code for them to be reused.
C++:

INDEX
S.No Topic Page No.

01. Introduction to c++ 1

02. C++ fundamentals 2-3

03. C++ Programming 4

04. Structure of C++ program 5-7

05. If Else Statement 8-10

06. Iteration Statements 11-19

07. Function 20-23

08. Pointer 24-26

09. Array 27-29

10. Linked List 30-43


C:

INDEX
S.No Topic Page No.

01. Introduction to c 44

02. C fundamentals 44

03. C Programming 45

04. Structure of C program 45-46

05. If Else Statement 47-48

06. Iteration Statements 49-53

07. Function 54-56

08. Array 56-57

09. Pointer 58-59

10. Student result management project 60-71

11. Conclusion 72
Introduction to C++

C++ is not a purely object-oriented language but a hybrid that contains the
functionality of the C programming language. The features that are available in C:
■ universally usable modular programs
■ efficient close to the machine programming
■ portable programs for various platforms.

Case Sensitivity:
C++ is case sensitive. In other words, uppercase and lowercase letters
are considered to be different.

C++ supports the concepts of object-oriented programming which are:


■ data abstraction, that is, the creation of classes to describe objects
■ data encapsulation for controlled access to object data
■ inheritance by creating derived classes
■ polymorphism i.e. the implementation of instructions that can have varying
effects during program execution.

Objects
Object-oriented programming shifts the focus of attention to the objects, that is, to
the aspects on which the problem is centered .OOP objects combine data and
functions (capacities). A class defines a certain object type by defining both the
properties and the capacities of the objects of that type.

Advantages of OOP
Object-oriented programming offers several major advantages to software
development:
■ reduced susceptibility to errors: an object controls access to its own data. More
specifically, an object can reject erroneous access attempts.
■ easy re-use: objects maintain themselves and can therefore be used as building
blocks for other programs.
■ low maintenance requirement: an object type can modify its own internal data
representation without requiring changes to the application.

1
C++ Fundamentals
The following three steps are required to create and translate a C++ program:

1. First, a text editor is used to save the C++ program in a text file. In other words,
the source code is saved to a source file. In larger projects the programmer will
normally use modular programming. This means that the source code will be
stored in several source files that are edited and translated separately.

2. The source file is put through a compiler for translation. If everything works as
planned, an object file made up of machine code is created. The object file is also
referred to as a module.

3. Finally, the linker combines the object file with other modules to form an
executable file.

It is important to use the correct file extension for the source file’s name. Although
the file extension depends on the compiler you use, the most commonly found file
extensions are .cpp and .cc.

Fundamental Types, Constants, and Variables

2
Variable:

In C++ a variable is a place to store information. A variable is a location in your


computer’s memory in which you can store a value and from which you can later
retrieve that value.

Keywords
Some words are reserved by C++, and you may not use them as variable names.
These are keywords used by the compiler to control your program. Keywords
include if, while, for, and main.

Characters
Character variables (type char) are typically 1 byte, enough to hold 256 values (see
Appendix C). A char can be interpreted as a small number (0–255) or as a member
of the ASCII set. ASCII stands for the American Standard Code for Information
Interchange. The ASCII character set and its ISO (International Standards
Organization) equivalent are a way to encode all the letters, numerals, and
punctuation marks.

Constants
Like variables, constants are data storage locations. Unlike variables, and as the
name implies, constants don’t change. You must initialize a constant when you
create it, and you cannot assign a new value later.
C++ has two types of constants: literal and symbolic.

Literal Constants
A literal constant is a value typed directly into your program wherever it is needed.
For example:
int myAge = 39;
myAge is a variable of type int; 39 is a literal constant. You can’t assign a value to
39, and its value can’t be changed.

Symbolic Constants
A symbolic constant is a constant that is represented by a name, just as a variable is
represented.
Unlike a variable, however, after a constant is initialized, its value can’t be
changed.

3
C++ Programming

Structure of a c++ program:

Program 1. Simple output

4
1.Header File:

#include <iostream>

Lines beginning with a hash sign (#) are directives for the preprocessor. They are
not regular code lines with expressions but indications for the compiler's
preprocessor. In this case the directive #include <iostream> tells the preprocessor
to include the iostream standard file. This specific file (iostream) includes the
declarations of the basic standard input-output library in C++, and it is included
because its functionality is going to be used later in the program.

2. using namespace std;


All the elements of the standard C++ library are declared within what is called a
namespace, the namespace with the name std. So in order to access its functionality
we declare with this expression that we will be using these entities.

3. Main function:

int main ()

This line corresponds to the beginning of the definition of the main function. The
main function is the point by where all C++ programs start their execution,
independently of its location within the source code.

The word main is followed in the code by a pair of parentheses (()). That is
because it is a function declaration: In C++, what differentiates a function
declaration from other types of expressions are these parentheses that follow its
name.

4. Body:

Right after these parentheses we can find the body of the main function enclosed in
braces ({}). What is contained within these braces is what the function does when
it is executed.

5
Program 2. Program to add two numbers
1. #include <iostream>
2.  
3. using namespace std;
4.  
5. int main()
6. {
7.    int a, b, c;
8.    
9.    cout << "Enter two integers to add\n";
10.    cin >> a >> b;
11.  
12.    c = a + b;
13.    cout <<"Sum of the numbers: " << c << endl;
14.    
15.    return 0;
16. }

Output:

6
If-Else:

Sometimes we need to execute a block of statements only when a particular


condition is met or not met. This is called decision making, as we are executing a
certain code after making a decision in the program logic. For decision making in
C++, we have four types of control statements (or control structures), which are as
follows:

a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

If statement in C++:

7
If else statement in C++:
Sometimes you have a condition and you want to execute a block of code if
condition is true and execute another piece of code if the same condition is false.
This can be achieved in C++ using if-else statement.

Program 3. Check Whether Number is Even or Odd using if else


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n;
7.
8. cout << "Enter an integer: ";
9. cin >> n;
10.
11. if ( n % 2 == 0)
12. cout << n << " This is an even number";
13. else
14. cout << n << " This is an odd number.";
15.
16. return 0;
8
17. }

Output:

Program 4. Check Whether Number is Prime or not using if else


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n, i;
7. bool isPrime = true;
8.
9. cout << "Enter the number: ";
10. cin >> n;
11.
12. for(i = 2; i <= n / 2; ++i)
13. {
14. if(n % i == 0)
15. {
16. isPrime = false;
17. break;
18. }
19. }
20. if (isPrime)
21. cout << " prime number";
22. else
23. cout << " not a prime number";
24.
25. return 0;
26. }

9
Output:

10
Itertion Statements:

A loop statement allows us to execute a statement or group of statements multiple


times and following is the general from of a loop statement in most of the
programming languages –

C++ programming language provides the following type of loops to handle looping
requirements. There are three type of loops in C++ programming:

1. for loop
2. while loop
3. do...while loop

11
For Loop:

How for loop works?


1. The initialization statement is executed only once at the beginning.
2. Then, the test expression is evaluated.
3. If the test expression is false, for loop is terminated. But if the test
expression is true, codes inside body of for loop is executed and update
expression is updated.
4. Again, the test expression is evaluated and this process repeats until the test
expression is false.

12
Program 5. Table of an integer:

 Display Multiplication table up to 10


1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n;
7.
8. cout << "Enter Number To Find its Multiplication
table ";
9. cin >> n;
10.
11. for (int i = 1; i <= 10; ++i) {
12. cout << n << " * " << i << " = " << n * i <<
endl;
13. }
14.
15. return 0;
16. }

Output:

13
While Loop:

How while loop works?


 The while loop evaluates the test expression.
 If the test expression is true, codes inside the body of while loop is
evaluated.
 Then, the test expression is evaluated again. This process goes on until the
test expression is false.
 When the test expression is false, while loop is terminated.

14
Program 6. Fibonacci series:
1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int n, t1 = 0, t2 = 1, nextTerm = 0;
7.
8. cout << "Enter The Number Of Terms: ";
9. cin >> n;
10.
11. cout << "The Fibonacci Series is: ";
12.
13. for (int i = 1; i <= n; ++i)
14. {
15. // Prints the first two terms.
16. if(i == 1)
17. {
18. cout << " " << t1;
19. continue;
20. }
21. if(i == 2)
22. {
23. cout << t2 << " ";
24. continue;
25. }
26. nextTerm = t1 + t2;
27. t1 = t2;
28. t2 = nextTerm;
29.
30. cout << nextTerm << " ";
31. }
32. return 0;
33. }

15
Output:

16
Do While Loop:

How do...while loop works?


 The codes inside the body of loop is executed at least once. Then, only the
test expression is checked.
 If the test expression is true, the body of loop is executed. This process
continues until the test expression becomes false.
 When the test expression is false, do...while loop is terminated.

17
Program 7.Armstrong Number :
#include<stdio.h>

#include<conio.h>

void main()

int n,c,d,s=0,num;

clrscr();

printf("\n Enter Any Number : ");

scanf("%d",&n);

num=n;

do

d=n%10;

c=d*d*d;

s=s+c;

n=n/10;

}while(n!=0);

if(s==num)

printf("\n %d is Armstrong Number",num);

else

printf("\n %d is Not an Armstrong Number",num);

getch();

18
Output:

19
Function:

A function is a group of statements that together perform a task. Every C++


program has at least one function, which is main(), and all the most trivial
programs can define additional functions.

A function declaration tells the compiler about a function's name, return type, and


parameters. A function definition provides the actual body of the function.

The C++ standard library provides numerous built-in functions that your program
can call. For example, function strcat() to concatenate two strings,
function memcpy() to copy one memory location to another location and many
more functions.

A C++ function definition consists of a function header and a function body. Here
are all the parts of a function −
 Return Type − A function may return a value. The return_type is the data
type of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the
keyword void.
 Function Name − This is the actual name of the function. The function
name and the parameter list together constitute the function signature.
 Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
 Function Body − The function body contains a collection of statements that
define what the function does.

Types of function
We have two types of function in C++:

20
1) Build-in functions

Built-in functions are also known as library functions. We need not to declare and
define these functions as they are already written in the C++ libraries such as
iostream, cmath etc. We can directly call them when we need.

2) User-defined functions

 The functions that we declare and write in our programs are user-defined
functions.

21
Program 8.Check if the given no is palindrome or not using function:

#include<iostream>
using namespace std;

int checkNumber(int n) {

int r, rev = 0;

while (n > 0) {
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
return rev;
}

int main() {

int n, num;

cout << "Enter Number :";


cin>>n;

num = checkNumber(n);
if (num == n) {
cout << "\n Number is Palindrome :" << n;
} else {
cout << "\n Number is Not Palindrome :" << n;
}

return 0;
}

22
Output:

23
Pointers:
Pointers are powerful features of C++ that differentiates it from other programming
languages like Java and Python.
Pointers are used in C++ program to access the memory and manipulate the
address.

Address in C++:

To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's
memory. The value the variable stores is actually stored in the location assigned.
To know where the data is stored, C++ has an & operator. The & (reference)
operator gives you the address occupied by a variable. If var is a variable
then, &var gives the address of that variable.

Following are the valid pointer declaration –

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

Using Pointers in C++:

There are few important operations, which we will do with the pointers very
frequently. (a) We define a pointer variable. (b) Assign the address of a variable to
a pointer. (c) Finally access the value at the address available in the pointer
variable. This is done by using unary operator * that returns the value of the
variable located at the address specified by its operand. 

24
Program 9. Program to Swap Elements Using Call by Reference:

1. #include<iostream>
2. using namespace std;
3.
4. void cyclicSwap(int *a, int *b, int *c);
5.
6. int main()
7. {
8. int a, b, c;
9.
10. cout << "Enter value of a, b and c respectively: ";
11. cin >> a >> b >> c;
12.
13. cout << "Value before swapping: " << endl;
14. cout << "a, b and c respectively are: " << a << ", "
<< b << ", " << c << endl;
15.
16. cyclicSwap(&a, &b, &c);
17.
18. cout << "Value after swapping numbers in cycle: " <<
endl;
19. cout << "a, b and c respectively are: " << a << ", "
<< b << ", " << c << endl;
20.
21. return 0;
22. }
23.
24. void cyclicSwap(int *a, int *b, int *c)
25. {
26. int temp;
27. temp = *b;
28. *b = *a;
29. *a = *c;
30. *c = temp;
31. }

25
Output:

Enter value of a, b and c respectively: 1

Value before swapping:

a=1

b=2

c=3

Value after swapping numbers in cycle:

a=3

b=1

c=2

26
Array:

C++ provides a data structure, the array, which stores a fixed-size sequential


collection of elements of the same type. An array is used to store a collection of
data, but it is often more useful to think of an array as a collection of variables of
the same type.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific
element in an array is accessed by an index.

Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements and
the number of elements required by an array as follows –
type arrayName [ arraySize ];

Arrays in C++:

Arrays are important to C++ and should need lots of more detail. There are
following few important concepts, which should be clear to a C++ programmer –

Sr.N Concept & Description


o

1 Multi-dimensional arrays
C++ supports multidimensional arrays. The simplest form of the
multidimensional array is the two-dimensional array.

2 Pointer to an array
You can generate a pointer to the first element of an array by simply
specifying the array name, without any index.

3 Passing arrays to functions


You can pass to the function a pointer to an array by specifying the
array's name without an index.

4 Return array from functions


C++ allows a function to return an array.

27
Program 10. Find Transpose of a Matrix

1. #include <iostream>
2. using namespace std;
3.
4. int main()
5. {
6. int a[10][10], trans[10][10], r, c, i, j;
7.
8. cout << "Enter rows and columns of matrix: ";
9. cin >> r >> c;
10.
11. // Storing element of matrix entered by user in array a[][].
12. cout << endl << "Enter elements of matrix: " << endl;
13. for(i = 0; i < r; ++i)
14. for(j = 0; j < c; ++j)
15. {
16. cout << "Enter elements a" << i + 1 << j + 1 << ": ";
17. cin >> a[i][j];
18. }
19.
20. // Displaying the matrix a[][]
21. cout << endl << "Entered Matrix: " << endl;
22. for(i = 0; i < r; ++i)
23. for(j = 0; j < c; ++j)
24. {
25. cout << " " << a[i][j];
26. if(j == c - 1)
27. cout << endl << endl;
28. }
29.
30. // Finding transpose of matrix a[][] and storing it in array trans[]
[].
31. for(i = 0; i < r; ++i)
32. for(j = 0; j < c; ++j)
33. {
34. trans[j][i]=a[i][j];
35. }
36.
37. // Displaying the transpose,i.e, Displaying array trans[][].
38. cout << endl << "Transpose of Matrix: " << endl;
39. for(i = 0; i < c; ++i)
40. for(j = 0; j < r; ++j)
41. {
42. cout << " " << trans[i][j];
43. if(j == r - 1)
44. cout << endl << endl;
45. }
46.
47. return 0;
48. }
Output:
28
Enter rows and column of matrix: 2
3

Enter elements of matrix:


Enter elements a11: 1
Enter elements a12: 2
Enter elements a13: 9
Enter elements a21: 0
Enter elements a22: 4
Enter elements a23: 7

Entered Matrix:
1 2 9

0 4 7

Transpose of Matrix:
1 0

2 4

9 7
Linked Lists:
29
A linked list  is a series of connected "nodes" that contains the "address" of the
next node. Each node can store a data point which may be a number, a string or
any other type of data.

Utility of Linked List:

Lists are one of the most popular and efficient data structures, with implementation
in every programming language like C, C++, Python, Java and C#.
Apart from that, linked lists are a great way to learn how pointers work. By
practicing how to manipulate linked lists, you can prepare yourself to learn more
advanced data structures like graphs and trees.

Types of Linked List -


There are three common types of Linked List.
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List

Singly Linked List


It is the most common. Each node has data and a pointer to the next node.
Node is represented as:

1. struct node {
2. int data;
3. struct node *next;
4. }
Doubly Linked List 30

We add a pointer to the previous node in a doubly linked list. Thus, we can go in
either direction: forward or backward. A node is represented as

1. struct node {
2. int data;
3. struct node *next;
4. struct node *prev;
5. }

Circular Linked List

A circular linked list is a variation of linked list in which the last element is linked
to the first element. This forms a circular loop. A circular linked list can be either
singly linked or doubly linked.

 for singly linked list, next pointer of last item points to the first item

 In doubly linked list, prev pointer of first item points to last item as well.

31
Program 11. C++ Program To Implement Singly Linked List:

 The program output is also shown below.


1. /*
2.   * C++ Program to Implement Singly Linked List
3.   */
4. #include<iostream>
5. #include<cstdio>
6. #include<cstdlib>
7. using namespace std;
8. /*
9.   * Node Declaration
10.   */
11. struct node
12. {
13. int info;
14. struct node *next;
15. }*start;
16.  
17. /*
18.   * Class Declaration
19.   */
20. class single_llist
21. {
22. public:
23. node* create_node(int);
24. void insert_begin();
25. void insert_pos();
26. void insert_last();
27. void delete_pos();
28. void sort();
29. void search();
30. void update();
31. void reverse();
32. void display();
33. single_llist()
34. {
35. start = NULL;
36. }
37. }; 32
38.  
39. /*
40.   * Main :contains menu
41.   */
42. main()
43. {
44. int choice, nodes, element, position, i;
45. single_llist sl;
46. start = NULL;
47. while (1)
48. {
49. cout<<endl<<"---------------------------
------"<<endl;
50. cout<<endl<<"Operations on singly linked
list"<<endl;
51. cout<<endl<<"---------------------------
------"<<endl;
52. cout<<"1.Insert Node at
beginning"<<endl;
53. cout<<"2.Insert node at last"<<endl;
54. cout<<"3.Insert node at position"<<endl;
55. cout<<"4.Sort Link List"<<endl;
56. cout<<"5.Delete a Particular
Node"<<endl;
57. cout<<"6.Update Node Value"<<endl;
58. cout<<"7.Search Element"<<endl;
59. cout<<"8.Display Linked List"<<endl;
60. cout<<"9.Reverse Linked List "<<endl;
61. cout<<"10.Exit "<<endl;
62. cout<<"Enter your choice : ";
63. cin>>choice;
64. switch(choice)
65. {
66. case 1:
67. cout<<"Inserting Node at Beginning:
"<<endl;
68. sl.insert_begin();
69. cout<<endl;
70. break;
71. case 2: 33
72. cout<<"Inserting Node at Last:
"<<endl;
73. sl.insert_last();
74. cout<<endl;
75. break;
76. case 3:
77. cout<<"Inserting Node at a given
position:"<<endl;
78. sl.insert_pos();
79. cout<<endl;
80. break;
81. case 4:
82. cout<<"Sort Link List: "<<endl;
83. sl.sort();
84. cout<<endl;
85. break;
86. case 5:
87. cout<<"Delete a particular node:
"<<endl;
88. sl.delete_pos();
89. break;
90. case 6:
91. cout<<"Update Node Value:"<<endl;
92. sl.update();
93. cout<<endl;
94. break;
95. case 7:
96. cout<<"Search element in Link List:
"<<endl;
97. sl.search();
98. cout<<endl;
99. break;
100. case 8:
101. cout<<"Display elements of link
list"<<endl;
102. sl.display();
103. cout<<endl;
104. break;
34
105. case 9:
106. cout<<"Reverse elements of Link
List"<<endl;
107. sl.reverse();
108. cout<<endl;
109. break;
110. case 10:
111. cout<<"Exiting..."<<endl;
112. exit(1);
113. break;
114. default:
115. cout<<"Wrong choice"<<endl;
116. }
117. }
118. }
119.  
120. /*
121.   * Creating Node
122.   */
123. node *single_llist::create_node(int value)
124. {
125. struct node *temp, *s;
126. temp = new(struct node);
127. if (temp == NULL)
128. {
129. cout<<"Memory not allocated "<<endl;
130. return 0;
131. }
132. else
133. {
134. temp->info = value;
135. temp->next = NULL;
136. return temp;
137. }
138. }
139.  
140. /*
141.   * Inserting element in beginning
142.   */
143. void single_llist::insert_begin()
35
144. {
145. int value;
146. cout<<"Enter the value to be inserted: ";
147. cin>>value;
148. struct node *temp, *p;
149. temp = create_node(value);
150. if (start == NULL)
151. {
152. start = temp;
153. start->next = NULL;
154. }
155. else
156. {
157. p = start;
158. start = temp;
159. start->next = p;
160. }
161. cout<<"Element Inserted at beginning"<<endl;
162. }
163.  
164. /*
165.   * Inserting Node at last
166.   */
167. void single_llist::insert_last()
168. {
169. int value;
170. cout<<"Enter the value to be inserted: ";
171. cin>>value;
172. struct node *temp, *s;
173. temp = create_node(value);
174. s = start;
175. while (s->next != NULL)
176. {
177. s = s->next;
178. }
179. temp->next = NULL;
180. s->next = temp;
181. cout<<"Element Inserted at last"<<endl;
182. } 36
183.  
184. /*
185.   * Insertion of node at a given position
186.   */
187. void single_llist::insert_pos()
188. {
189. int value, pos, counter = 0;
190. cout<<"Enter the value to be inserted: ";
191. cin>>value;
192. struct node *temp, *s, *ptr;
193. temp = create_node(value);
194. cout<<"Enter the postion at which node to be
inserted: ";
195. cin>>pos;
196. int i;
197. s = start;
198. while (s != NULL)
199. {
200. s = s->next;
201. counter++;
202. }
203. if (pos == 1)
204. {
205. if (start == NULL)
206. {
207. start = temp;
208. start->next = NULL;
209. }
210. else
211. {
212. ptr = start;
213. start = temp;
214. start->next = ptr;
215. }
216. }
217. else if (pos > 1 && pos <= counter)
218. {
219. s = start;
220. for (i = 1; i 37
< pos; i++)
221. {
222. ptr = s;
223. s = s->next;
224. }
225. ptr->next = temp;
226. temp->next = s;
227. }
228. else
229. {
230. cout<<"Positon out of range"<<endl;
231. }
232. }
233.  
234. /*
235.   * Sorting Link List
236.   */
237. void single_llist::sort()
238. {
239. struct node *ptr, *s;
240. int value;
241. if (start == NULL)
242. {
243. cout<<"The List is empty"<<endl;
244. return;
245. }
246. ptr = start;
247. while (ptr != NULL)
248. {
249. for (s = ptr->next;s !=NULL;s = s->next)
250. {
251. if (ptr->info > s->info)
252. {
253. value = ptr->info;
254. ptr->info = s->info;
255. s->info = value;
256. }
257. }
258. ptr = ptr->next;
38
259. }
260. }
261.  
262. /*
263.   * Delete element at a given position
264.   */
265. void single_llist::delete_pos()
266. {
267. int pos, i, counter = 0;
268. if (start == NULL)
269. {
270. cout<<"List is empty"<<endl;
271. return;
272. }
273. cout<<"Enter the position of value to be
deleted: ";
274. cin>>pos;
275. struct node *s, *ptr;
276. s = start;
277. if (pos == 1)
278. {
279. start = s->next;
280. }
281. else
282. {
283. while (s != NULL)
284. {
285. s = s->next;
286. counter++;
287. }
288. if (pos > 0 && pos <= counter)
289. {
290. s = start;
291. for (i = 1;i < pos;i++)
292. {
293. ptr = s;
294. s = s->next;
295. }
296. ptr->next = s->next;
297. } 39
298. else
299. {
300. cout<<"Position out of range"<<endl;
301. }
302. free(s);
303. cout<<"Element Deleted"<<endl;
304. }
305. }
306.  
307. /*
308.   * Update a given Node
309.   */
310. void single_llist::update()
311. {
312. int value, pos, i;
313. if (start == NULL)
314. {
315. cout<<"List is empty"<<endl;
316. return;
317. }
318. cout<<"Enter the node postion to be updated:
";
319. cin>>pos;
320. cout<<"Enter the new value: ";
321. cin>>value;
322. struct node *s, *ptr;
323. s = start;
324. if (pos == 1)
325. {
326. start->info = value;
327. }
328. else
329. {
330. for (i = 0;i < pos - 1;i++)
331. {
332. if (s == NULL)
333. {
334. cout<<"There are less than
"<<pos<<" elements"; 40
335. return;
336. }
337. s = s->next;
338. }
339. s->info = value;
340. }
341. cout<<"Node Updated"<<endl;
342. }
343.  
344. /*
345.   * Searching an element
346.   */
347. void single_llist::search()
348. {
349. int value, pos = 0;
350. bool flag = false;
351. if (start == NULL)
352. {
353. cout<<"List is empty"<<endl;
354. return;
355. }
356. cout<<"Enter the value to be searched: ";
357. cin>>value;
358. struct node *s;
359. s = start;
360. while (s != NULL)
361. {
362. pos++;
363. if (s->info == value)
364. {
365. flag = true;
366. cout<<"Element "<<value<<" is found
at position "<<pos<<endl;
367. }
368. s = s->next;
369. }
370. if (!flag)
371. cout<<"Element "<<value<<" not found in
the list"<<endl;
372. }
373.  
374. /*
375.   * Reverse Link List
376.   */
377. void single_llist::reverse()
378. {
379. struct node *ptr1, *ptr2, *ptr3;
380. if (start == NULL)
381. {
382. cout<<"List is empty"<<endl;
383. return;
384. }
385. if (start->next == NULL)
386. {
387. return;
388. }
389. ptr1 = start;
390. ptr2 = ptr1->next;
391. ptr3 = ptr2->next;
392. ptr1->next = NULL;
393. ptr2->next = ptr1;
394. while (ptr3 != NULL)
395. {
396. ptr1 = ptr2;
397. ptr2 = ptr3;
398. ptr3 = ptr3->next;
399. ptr2->next = ptr1;
400. }
401. start = ptr2;
402. }
403.  
404. /*
405.   * Display Elements of a link list
406.   */
407. void single_llist::display()
408. {
409. struct node *temp;
410. if (start == NULL)42
411. {
412. cout<<"The List is Empty"<<endl;
413. return;
414. }
415. temp = start;
416. cout<<"Elements of list are: "<<endl;
417. while (temp != NULL)
418. {
419. cout<<temp->info<<"->";
420. temp = temp->next;
421. }
422. cout<<"NULL"<<endl;
423. }

Output:

1.Insert Node at beginning


2.Insert node at last
3.Insert node at position
4.Sort Link List
5.Delete a Particular Node
6.Update Node Value
7.Search Element
8.Display Linked List
9.Reverse Linked List
10.Exit
Enter your choice : 1
Inserting Node at Beginning:
Enter the value to be inserted: 100
Element Inserted at beginning

Introduction
43 to C
C is a high-level and general-purpose programming language that is ideal for
developing firmware or portable applications. Originally intended for writing
system software, C was developed at Bell Labs by Dennis Ritchie for the Unix
Operating System in the early 1970s. Ranked among the most widely used
languages, C has a compiler for most computer systems and has influenced many
popular languages – notably C++.

Structure of a C program:

The components of the above structure are:


1. Header Files Inclusion: A header file is a file with extension .h which
contains C function declarations and macro definitions to be shared between
several source files.

Syntax to include a header file in C:


#include <(header_file_name).h>
2. Main Method Declaration: The next part of a C program is to declare the
main() function. The syntax to declare the main function is:

Syntax to Declare main method: 44


int main()
{}
3. Variable Declaration: The next part of any C program is the variable
declaration. It refers to the variables that are to be used in the function. Please
note that in C program, no variable can be used without being declared. Also
in a C program, the variables are to be declared before any operation in the
function.

Example:
int main()
{
int a;
.
.
4. Body: Body of a function in C program, refers to the operations that are
performed in the functions. It can be anything like manipulations, searching,
sorting, printing, etc.
Example:
int main()
{
int a;

printf("%d", a);
.
.
5. Return Statement: The last part in any C program is the return statement.
The return statement refers to the returning of the values from a function. This
return statement and return value depend upon the return-type of the function.
For example, if the return type is void, then there will be no return statement.
In any other case, there will be a return statement and the return value will be
of the type of the specified return-type.

Example:
int main() 45

{
int a;

printf("%d", a);

return 0;
}
if Statement
46
The if statement evaluates the test expression inside the parenthesis ().

 If the test expression is evaluated to true, statements inside the body of if are
executed.
 If the test expression is evaluated to false, statements inside the body
of if are not executed.

The syntax of the if statement in C programming is:


1. if (test expression)
2. {
3. // statements to be executed if the test expression is true
4. }

if...else Statement
If the test expression is evaluated to true:
 statements inside the body of if are executed.
 statements inside the body of else are skipped from execution.

If the test expression is evaluated to false,


 statements inside the body of else are executed
 statements inside the body of if are skipped from execution.

The if statement may have an optional else block. The syntax of


the if..else statement is:

1. if (test expression) {
2. // statements to be executed if the test expression is true
3. }
4. else {
5. // statements to be executed if the test expression is false
6. }
47
Program 1: if...else statement
1. // Check whether an integer is odd or even
2.
3. #include <stdio.h>
4. int main()
5. {
6. int number;
7. printf("Enter an integer: ");
8. scanf("%d", &number);
9.
10. // True if the remainder is 0
11. if (number%2 == 0)
12. {
13. printf("%d is an even integer.",number);
14. }
15. else
16. {
17. printf("%d is an odd integer.",number);
18. }
19.
20. return 0;
21. }

Output:
Enter an integer: 7
7 is an odd integer.
ITERATION STATEMENTS: 48

C for Loop:
Loops are used in programming to execute a block of code repeatedly until a
specified condition is met.
C programming has three types of loops:
1. for loop
2. while loop
3. do...while loop

For Loop
The syntax of the for loop is:
1. for (initializationStatement; testExpression; updateStatement)
2. {
3. // statements inside the body of loop
4. }
Program 2: for loop
1. # Print numbers from 1 to 10 49
2. #include <stdio.h>
3.
4. int main() {
5. int i;
6.
7. for (i = 1; i < 11; ++i)
8. {
9. printf("%d ", i);
10. }
11. return 0;
12. }

Output
1 2 3 4 5 6 7 8 9 10
while loop
The syntax of the while loop is:
1. while (testExpression)
2. {
3. // statements inside the body of the loop
4. }

Program 3: while loop


1. // Print numbers from 1 to 5 50
2.
3. #include <stdio.h>
4. int main()
5. {
6. int i = 1;
7.
8. while (i <= 5)
9. {
10. printf("%d\n", i);
11. ++i;
12. }
13.
14. return 0;
15. }

Output
1
2
3
4
5

do...while loop
The do..while loop is similar to the while loop with one important difference. The
body of do...while loop is executed at least once. Only then, the test expression is
evaluated.
The syntax of the do...while loop is:
1. do
2. {
3. // statements inside the body of the loop
4. }
5. while (testExpression);

51`

Program 4: do...while loop


1. // Program to add numbers until the user enters zero
2.
3. #include <stdio.h>
4. int main()
5. {
6. double number, sum = 0;
7.
8. // the body of the loop is executed at least once
9. do
10. {
11. printf("Enter a number: ");
12. scanf("%lf", &number);
13. sum += number;
14. }
15. while(number != 0.0);
16.
17. printf("Sum = %.2lf",sum);
18.
19. return 0;
20. }

Output 52

Enter a number: 1.5


Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
Functions
A function is a block of code that performs 53
a specific task. Dividing complex
problem into small components makes program easy to understand and use.
Types of function

Depending on whether a function is defined by the user or already included in C


compilers, there are two types of functions in C programming

There are two types of function in C programming:

 Standard library functions


 User defined functions
Program 5: Function

1 #include<stdio.h> 54

2 // function prototype, also called function declaration

3 void swap(int a, int b);          

4  

5 int main()

6 {

7     int m = 22, n = 44;

8     // calling swap function by value


9     printf(" values before swap  m = %d \nand n = %d", m, n);

10     swap(m, n);                        

11 }

12  

13 void swap(int a, int b)

14 {

15     int tmp;

16     tmp = a;

17     a = b;

18     b = tmp;

19     printf(" \nvalues after swap m = %d\n and n = %d", a, b);

20 }

OUTPUT:
values before swap m = 22
and n = 44
values after swap m = 44
and n = 22

Arrays

An array is a collection of a fixed number of values of a single type. For example:


if you want to store 100 integers in sequence, you can create an array for it.
int data[100];

The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

1. One-dimensional arrays
2. Multidimensional arrays (will be discussed in next chapter)
How to declare arrays?

data_type array_name[array_size];

Program 6: Arrays
1. // Program to find the average of n (n < 10) numbers using arrays
2.
3. #include <stdio.h>
4. int main()
5. {
6. int marks[10], i, n, sum = 0, average;
7. printf("Enter n: ");
8. scanf("%d", &n);
9. for(i=0; i<n; ++i)
10. {
11. printf("Enter number%d: ",i+1);
12. scanf("%d", &marks[i]);
13. sum += marks[i];
14. }
15. average = sum/n;
16.
17. printf("Average = %d", average);
18.
19. return 0;
20. }

Output
Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39

 Pointers 57

Pointers are powerful features of C and (C++) programming that differentiates it


from other popular programming languages like Java and Python. Pointers are used
in C program to access the memory and manipulate the address.

Pointer variables:

In C, you can create a special variable that stores the address (rather than the
value). This variable is called pointer variable or simply a pointer.

How to create a pointer variable?


data_type* pointer_variable_name;

int* p;
Program 7: Swap two numbers using call by reference (address)
#include <stdio.h>

void swap(int *a,int *b)

{
int t;
t = *a;
*a = *b;
*b = t;
}
int main()
{
int num1,num2;
printf("Enter value of num1: ");
scanf("%d",&num1);
printf("Enter value of num2: ");
scanf("%d",&num2);
//print values before swapping
printf("Before Swapping: num1=%d, num2=%d\n",num1,num2);
//call function by passing addresses of num1 and num2
swap(&num1,&num2);
//print values after swapping
printf("After Swapping: num1=%d, num2=%d\n",num1,num2);
return 0;
}

Output

Enter value of num1: 10


Enter value of num2: 20
Before Swapping: num1=10, num2=20
After Swapping: num1=20, num2=10

Student Report Card System


Project in C++
Student report card system project in C++ is a simple console application built
without the use of graphics. In this project, users can perform typical report card
related functions like adding a new student record and displaying, modifying,
editing and deleting it. File handling has been effectively used to perform all these.
The key features of Student Report Card System are:

1. Create student report card record

2. Read all students report card record

3. Read specific student’s report card record

4. Display all students’ grade report


5. Modify student’s report card record

6. Delete student record:

//***************************************************************
// HEADER FILE USED IN 60
PROJECT
//****************************************************************

#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<fstream.h>
#include<iomanip.h>

//***************************************************************
// CLASS USED IN PROJECT
//****************************************************************

class student
{
int rollno;
char name[50];
int p_marks,c_marks,m_marks,e_marks,cs_marks;
float per;
char grade;
int std;
void calculate()
{
per=(p_marks+c_marks+m_marks+e_marks+cs_marks)/5.0;
if(per>=60)
grade='A';
else if(per>=50 && per<60)
grade='B';
else if(per>=33 && per<50)
grade='C';
else
grade='F';
}
public:
void getdata()
{
cout<<"\nEnter The roll number of student ";
cin>>rollno;
cout<<"\n\nEnter The Name of student ";
gets(name);
cout<<"\nEnter The marks in physics out of 100 : ";
cin>>p_marks; 61
cout<<"\nEnter The marks in chemistry out of 100 : ";
cin>>c_marks;
cout<<"\nEnter The marks in maths out of 100 : ";
cin>>m_marks;
cout<<"\nEnter The marks in english out of 100 : ";
cin>>e_marks;
cout<<"\nEnter The marks in computer science out of 100 : ";
cin>>cs_marks;
calculate();
}

void showdata()
{
cout<<"\nRoll number of student : "<<rollno;
cout<<"\nName of student : "<<name;
cout<<"\nMarks in Physics : "<<p_marks;
cout<<"\nMarks in Chemistry : "<<c_marks;
cout<<"\nMarks in Maths : "<<m_marks;
cout<<"\nMarks in English : "<<e_marks;
cout<<"\nMarks in Computer Science :"<<cs_marks;
cout<<"\nPercentage of student is :"<<setprecision(2)<<per;
cout<<"\nGrade of student is :"<<grade;
}

void show_tabular()
{

cout<<rollno<<setw(12)<<name<<setw(10)<<p_marks<<setw(3)<<c_marks<<set
w(3)<<m_marks<<setw(3)<<e_marks<<setw(3)<<cs_marks<<setw(6)<<setprecis
ion(3)<<per<<" "<<grade<<endl;
}

int retrollno()
{
return rollno;
}

}; //class ends here


62

//***************************************************************
// global declaration for stream object, object
//****************************************************************

fstream fp;
student st;

//***************************************************************
// function to write in file
//****************************************************************

void write_student()
{
fp.open("student.dat",ios::out|ios::app);
st.getdata();
fp.write((char*)&st,sizeof(student));
fp.close();
cout<<"\n\nstudent record Has Been Created ";
getch();
}

//***************************************************************
// function to read all records from file
//****************************************************************

void display_all()
{
clrscr();
cout<<"\n\n\n\t\tDISPLAY ALL RECORD !!!\n\n";
fp.open("student.dat",ios::in);
while(fp.read((char*)&st,sizeof(student)))
{
st.showdata();
cout<<"\n\n====================================\n";
getch();
}
63
fp.close();
getch();
}

//***************************************************************
// function to read specific record from file
//****************************************************************

void display_sp(int n)
{
int flag=0;
fp.open("student.dat",ios::in);
while(fp.read((char*)&st,sizeof(student)))
{
if(st.retrollno()==n)
{
clrscr();
st.showdata();
flag=1;
}
}
fp.close();
if(flag==0)
cout<<"\n\nrecord not exist";
getch();
}

//***************************************************************
// function to modify record of file
//****************************************************************

void modify_student()
{
int no,found=0;
clrscr();
cout<<"\n\n\tTo Modify "; 64
cout<<"\n\n\tPlease Enter The roll number of student";
cin>>no;
fp.open("student.dat",ios::in|ios::out);
while(fp.read((char*)&st,sizeof(student)) && found==0)
{
if(st.retrollno()==no)
{
st.showdata();
cout<<"\nPlease Enter The New Details of student"<<endl;
st.getdata();
int pos=-1*sizeof(st);
fp.seekp(pos,ios::cur);
fp.write((char*)&st,sizeof(student));
cout<<"\n\n\t Record Updated";
found=1;
}
}
fp.close();
if(found==0)
cout<<"\n\n Record Not Found ";
getch();
}

//***************************************************************
// function to delete record of file
//****************************************************************

void delete_student()
{
int no;
clrscr();
cout<<"\n\n\n\tDelete Record";
cout<<"\n\nPlease Enter The roll number of student You Want To Delete";
cin>>no;
fp.open("student.dat",ios::in|ios::out);
fstream fp2;
fp2.open("Temp.dat",ios::out);
fp.seekg(0,ios::beg);
while(fp.read((char*)&st,sizeof(student)))65
{
if(st.retrollno()!=no)
{
fp2.write((char*)&st,sizeof(student));
}
}
fp2.close();
fp.close();
remove("student.dat");
rename("Temp.dat","student.dat");
cout<<"\n\n\tRecord Deleted ..";
getch();
}

//***************************************************************
// function to display all students grade report
//****************************************************************
void class_result()
{
clrscr();
fp.open("student.dat",ios::in);
if(!fp)
{
cout<<"ERROR!!! FILE COULD NOT BE OPEN\n\n\n Go To Entry Menu
to create File";
cout<<"\n\n\n Program is closing ....";
getch();
exit(0);
}

cout<<"\n\n\t\tALL STUDENTS RESULT \n\n";

cout<<"====================================================\n
";
cout<<"Roll No. Name P C M E CS %age Grade\n";

cout<<"====================================================\n
66
";

while(fp.read((char*)&st,sizeof(student)))
{
st.show_tabular();
}
fp.close();
getch();
}

//***************************************************************
// function to display result menu
//****************************************************************

void result()
{
int ans,rno;
char ch;
clrscr();
cout<<"\n\n\nRESULT MENU";
cout<<"\n\n\n1. Class Result\n\n2. Student Report Card\n\n3.Back to Main
Menu";
cout<<"\n\n\nEnter Choice (1/2)? ";
cin>>ans;
switch(ans)
{
case 1 :
class_result();
break;
case 2 :
{
do
{
clrscr();
char ans;
cout<<"\n\nEnter Roll Number Of Student : ";
cin>>rno; 67
display_sp(rno);
cout<<"\n\nDo you want to See More Result (y/n)?";
cin>>ans;
}
while(ans=='y'||ans=='Y');

break;
}
case 3:
break;
default:
cout<<"\a";
}
}

//***************************************************************
// INTRODUCTION FUNCTION
//****************************************************************

void intro()
{
clrscr();
gotoxy(35,11);
cout<<"STUDENT";
gotoxy(33,14);
cout<<"REPORT CARD";
gotoxy(35,17);
cout<<"PROJECT";
cout<<"\n\nMADE BY : Code With C";
cout<<"\n\nCONTACT : codewithc.com";
getch();

//***************************************************************
68
// ENTRY / EDIT MENU FUNCTION
//****************************************************************
void entry_menu()
{
clrscr();
char ch2;
cout<<"\n\n\n\tENTRY MENU";
cout<<"\n\n\t1.CREATE STUDENT RECORD";
cout<<"\n\n\t2.DISPLAY ALL STUDENTS RECORDS";
cout<<"\n\n\t3.SEARCH STUDENT RECORD ";
cout<<"\n\n\t4.MODIFY STUDENT RECORD";
cout<<"\n\n\t5.DELETE STUDENT RECORD";
cout<<"\n\n\t6.BACK TO MAIN MENU";
cout<<"\n\n\tPlease Enter Your Choice (1-6) ";
ch2=getche();
switch(ch2)
{
case '1':
clrscr();
write_student();
break;
case '2':
display_all();
break;
case '3':
int num;
clrscr();
cout<<"\n\n\tPlease Enter The roll number ";
cin>>num;
display_sp(num);
break;
case '4':
modify_student();
break;
case '5':
delete_student();
break;
case '6':
break;
default:
cout<<"\a"; 69
entry_menu();
}
}

//***************************************************************
// THE MAIN FUNCTION OF PROGRAM
//****************************************************************

void main()
{
char ch;
intro();
do
{
clrscr();
cout<<"\n\n\n\tMAIN MENU";
cout<<"\n\n\t01. RESULT MENU";
cout<<"\n\n\t02. ENTRY/EDIT MENU";
cout<<"\n\n\t03. EXIT";
cout<<"\n\n\tPlease Select Your Option (1-3) ";
ch=getche();
switch(ch)
{
case '1':
clrscr();
result();
break;
case '2':
entry_menu();
break;
case '3':
exit(0);
default :
cout<<"\a";
}
}
while(ch!='3');
} 70

//***************************************************************
// END OF PROJECT
//***************************************************************
CONCLUSION
71

I can honestly say that my time spent interning with Dreamtech resulted in one of
the best summers of my life.  Not only did I gain practical skills but I also had the
opportunity to meet many fantastic people. The atmosphere at the institution was
always welcoming which made me feel right at home.
This was a great experience to interact with everyone in an informal setting outside
of work.
Overall, my internship at DREAMTECH has been a success. I was able to gain
practical skills, work in a fantastic environment, and make connections that will
last a lifetime. I could not be more thankful.

You might also like