C Programming Manual
C Programming Manual
1 Simple Calculator 1
2 Quadratic Equation 3
3 Electricity Bill 5
4 Printing Pattern 10
5 Binary Search 12
6 Matrix Multiplication 14
7 Compute Sine and Cosine of an Angle 18
8 Bubble Sort 20
9 String Operations 22
10 C Structures 26
11 Pointers and Arrays 29
12 File Copy 31
i
Listings
ii
Preface
Use and spread the word of Free Software. Free Software leads to a Free Society!
Prabodh C P
iii
CODE::BLOCKS
CODE::BLOCKS TUTORIAL
Using Code::Blocks to compile and execute a C Program
To launch Code::Blocks IDE, Click on
Applications → Programming → Code::Blocks IDE
You will get a window as shown below
iv
LISTINGS LISTINGS
Code::Blocks creates what is called a Workspace to keep track of the project you are working on. It is possible for you to be
working on multiple projects within your workspace. A project is a collection of one or more source (as well as header) files.
Source files are the files that contain the source code for your program. If you are developing a C program, you are writing C
source code (.c files).
In the next step give the Project Title and specify the folder where you want to save your project. (Note :
Dont use any special characters or whitespaces for project title and folder names)
Finally you will be prompted to choose the compiler. Just choose the default options here (Dont change the options). You
should be using GNU GCC Compiler. Click Finish to create the new project.
The system will then return to the [MyProgram] window and you are ready to write your program.In the Man- agement
area of the screen (Shift-F2 toggles the Management display), you will see the files that are part of the project in the
Projects tab. To see the source files, click on the triangle symbol to expand the Workspace and its subdirectories.
Under Sources, there is a file called main.c, which is automatically created for you when you build a console application.
Click on main.c.
main.c contains a simple Hello World program which you can edit later to solve a programming problem. Now let us see
how to compile and execute this main.c program. Just to understand the process of debugging we knowingly introduce an
error in the program by removing the semicolon after the printf statement. We will now compile the program (To compile a
file means to take the instructions that you have written and translate it into machine code
The error messages are shown in the Build messages window at the bottom. Let us now try to understand these error
messages. === Build: Debug in MyProgram (compiler: GNU GCC Compiler) ===
main.c In function main:
main.c 7 error: expected ; before return
main.c 8 warning: control reaches end of non-void function [-Wreturn-type]
=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===
The error messages show the errors in syntax (and not the logical errors). It also indicates the line number and the type
of error. Here in this example the error says that before the return statement in line no 7 a semicolon is missing. The next
message is a warning message which has resulted because of the previous error.
Now go to line number 6 and add a semicolon at the end. Now build your project again.
Now the build message window shows the following message. 0 error(s), 0 warning(s) (0 minute(s), 0 second(s) This
means that the errors and warnings have been successfully resolved. Now it is time to run the program. You can Execute
the project from the Build pull-down menu by clicking on Run option[Ctrl+F10].
An output window pops displaying the output of the program. A greeting message ”Hello world!” is printed on to the
output console.
Simple Calculator
Question
Simulation of a Simple Calculator.
C Code
1 /***************************************************************************
2 *File : A01Calculator.c
3 *Description : Program to simulate a commercial calculator
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11 #include<math.h>
12
13 /***************************************************************************
14 *Function : main
15 *Input parameters : no parameters
16 *RETURNS : 0 on success
17 *************************************************************************** /
18
19 int main()
20 {
21 int iChoice, iOperand1, iOperand2;
22 char cOperator;
23
24 for(;;)
25 {
26 printf("\nEnter the arithmetic expression(Do not add spaces in the
expression)\n");
27 scanf("%d%c%d", &iOperand1, &cOperator, &iOperand2);
28 switch(cOperator)
29 {
30 case ’+’: printf("\nResult = %d", iOperand1 + iOperand2);
31 break;
32
1
CHAPTER 1. SIMPLE CALCULATOR
40 break;
41 case ’%’: printf("\nResult = %d", iOperand1 % iOperand2);
42 break;
43
44 }
45 printf("\nPress 1 to continue and 0 to quit : ");
46 scanf("%d", &iChoice);
47 if(0==iChoice)
48 {
49 break;
50 }
51 }
52 return 0;
53 }
Listing 1.1: A01Calculator.c
Output
=================================
1 /***************************************
2 Enter the arithmetic expression
3 4+6
4
5 Result = 10
6 Press 1 to continue and 0 to quit : 1
7
11 Result = -7
12 Press 1 to continue and 0 to quit : 1
13
17 Result = 10
18 Press 1 to continue and 0 to quit : 1
19
23 Result = 0.8
24 Press 1 to continue and 0 to quit : 1
25
29 Result = 2
30 Press 1 to continue and 0 to quit : 1
31
35 Result = 3
36 Press 1 to continue and 0 to quit : 0
37 *************************************** /
Listing 1.2: OUTPUT
Quadratic Equation
Question
Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate mes- sages.
C Code
1 /***************************************************************************
2 *File : A02Quadratic.c
3 *Description : Program to find the roots of a Quadratic Equation
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11 #include<math.h>
12 /***************************************************************************
13 *Function : main
14 *Input parameters : no parameters
15 *RETURNS : 0 on success
16 *************************************************************************** /
17 int main(void)
18 {
19 float fA,fB,fC,fDesc,fX1,fX2,fRealp,fImagp;
20 int iState;
21
22 printf("\n************************************************************* ");
23 printf("\n*\tPROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION\t *\n");
24 printf("************************************************************* ");
25
3
CHAPTER 2. QUADRATIC EQUATION
Output
1 /***************************************
2 *************************************************************
3 * PROGRAM TO FIND ROOTS OF A QUADRATIC EQUATION *
4 *************************************************************
5 Enter the coefficients of a,b,c
6 1 -5 6
7
Electricity Bill
Question
An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paise per unit: for the
next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs. 100 as meter
charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a
program to read the name of the user, number of units consumed and print out the charges.
C Code
Implementation 1
1 /***************************************************************************
2 *File : A03ElectricBill.c
3 *Description : Program to perform a binary search on 1D Array
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8 #include<stdio.h>
9 #include<stdlib.h>
10 /***************************************************************************
11 *Function : main
12 *Input parameters : no parameters
13 *RETURNS : 0 on success
14 *************************************************************************** /
15 int main(void)
16 {
17 char cName[30];
18 int iUnits;
19 const int iMinCharge = 100;
20 const double dSlab1 = 0.8;
21 const double dSlab2 = 0.9;
22 const double dSlab3 = 1.0;
23 const double dSurcharge = 0.15;
24 double dBillAmount = 0.0;
25
5
CHAPTER 3. ELECTRICITY BILL
35 dBillAmount += (200*dSlab1)+((iUnits-200)*dSlab2);
36 }
37 else
38 {
39 dBillAmount += (200*dSlab1)+(100*dSlab2)+((iUnits-300)*dSlab3);
40 }
41 if(dBillAmount > 400)
42 {
43 dBillAmount += dBillAmount * dSurcharge;
44 }
45 printf("\nElectricity Bill\n===================================");
46 printf("\nCustomer Name\t: %s", cName);
47 printf("\nUnits Consumed\t: %d", iUnits);
48 printf("\nBill Amount\t: %0.2lf Rupees\n\n", dBillAmount);
49 return 0;
50 }
Listing 3.1: A03ElectricBill.c
Output 1
1 /***************************************
2 Enter the name of the customer : Ramesh
3 Enter the number of units consumed : 457
4
5 Electricity Bill
6 ===================================
7 Customer Name : Ramesh
8 Units Consumed : 457
9 Bill Amount : 583.05 Rupees
10
14 Electricity Bill
15 ===================================
16 Customer Name : Sayeed
17 Units Consumed : 150
18 Bill Amount : 220.00 Rupees
19
23 Electricity Bill
24 ===================================
25 Customer Name : Jaswinder
26 Units Consumed : 300
27 Bill Amount : 350.00 Rupees
28
32 Electricity Bill
33 ===================================
34 Customer Name : Shyam
35 Units Consumed : 182
36 Bill Amount : 245.60 Rupees
37 *************************************** /
Listing 3.2: OUTPUT
Implementation 2
1 /***************************************************************************
2 *File : A03ElectricBill_02.c
3 *Description : Program to perform a binary search on 1D Array
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8 #include<stdio.h>
9 #include<stdlib.h>
10 /***************************************************************************
11 *Function : main
12 *Input parameters : no parameters
13 *RETURNS : 0 on success
14 *************************************************************************** /
19 int main()
20 {
21 char stName[40];
22 int iNumUnits;
23 float fAmount;
24
30 printf("ELECTRIC BILL\n");
31 printf("%-25s%s\n", "Name : ",stName);
32 printf("%-25s%d units\n", "Units Consumed : ", iNumUnits);
33
60
Output 2
1 /***************************************
2 Enter Customer name:SHAM
3
12
39 *************************************** /
Listing 3.4: OUTPUT
Printing Pattern
Question
Write a C Program to display the following by reading the number of rows as input
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
C Code Implementation
1 /***************************************************************************
2 *File : A04Pattern.c
3 *Description : Program to print a Pattern
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 26 December 2022
7 *************************************************************************** /
8 #include<stdio.h>
9 #include<stdlib.h>
10 /***************************************************************************
11 *Function : main
12 *Input parameters : no parameters
13 *RETURNS : 0 on success
14 *************************************************************************** /
15
16 int main()
17 {
18 int iNum, iSp_cnt, iNum_cnt, iVal, i, j, k;
19 printf("Enter the number of rows : ");
20 scanf("%d", &iNum);
21 iSp_cnt = iNum - 1;
22 iNum_cnt = 1;
23
24 for(i=0;i<iNum;i++)
25 {
26 iVal = 1;
27 for(j=0;j<iSp_cnt;j++)
28 {
29 printf(" ");
30 }
31 for(k=0;k<iNum_cnt;k++)
10
CHAPTER 4. PRINTING PATTERN
32 {
33 if(k <= iNum_cnt/2)
34 {
35 printf("%d ", iVal);
36 iVal++;
37 }
38 else
39 {
40 iVal--;
41 printf("%d ", iVal-1);
42 }
43 }
44 printf("\n");
45 iSp_cnt--;
46 iNum_cnt += 2;
47 }
48 return 0;
49 }
Listing 4.1: A04Pattern.c
Output
=================================
1 /****************************
2
Binary Search
Question
Implement Binary Search on Integers.
C Code
1 /***************************************************************************
2 *File : A05BinarySearchIntegers.c
3 * Description : Program to perform a binary search on 1D Array of Integers
4 * Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters : no parameters
15 *RETURNS : 0 on success
16 *************************************************************************** /
17
18 int main(void)
19 {
20 int iaArr[100],iNum,i,iMid,iLow,iHigh,iFound,iKey;
21
32 iFound = 0;
33 iLow = 0;
34 iHigh = iNum-1;
35 while(iLow <= iHigh)
36 {
37 iMid = (iLow+iHigh)/2;
38 if(iKey == iaArr[iMid])
39 {
40 iFound = 1;
41 break;
12
CHAPTER 5. BINARY SEARCH
42 }
43 else if(iKey < iaArr[iMid])
44 {
45 iHigh = iMid-1;
46 }
47 else
48 {
49 iLow = iMid+1;
50 }
51 }
52
53 if(iFound)
54 printf("\nKey element %d found at position %d\n",iKey,iMid+1);
55 else
56 printf("\nKey element not found\n");
57
58 return 0;
59 }
Listing 5.1: A05BinarySearchIntegers.c
Output
=================================
1 /****************************
2 Enter the number of elements
3 5
4
Matrix Multiplication
Question
Implement Matrix multiplication and validate the rules of multiplication.
C Code
1 /***************************************************************************
2 *File : A06MatrixMul.c
3 * Description : Program to implement Matrix Multiplication
4 * Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters : no parameters
15 *RETURNS : 0 on success
16 *************************************************************************** /
17
18 int main(void)
19 {
20 int iM, iN, iP, iQ, i, j, k, iaMat1[10][10], iaMat2[10][10];
21 int iaProd[10][10] = {0};
22
23
24 printf("\n********************************************************* ");
25 printf("\n*\tPROGRAM TO IMPLEMENT MATRIX MULIPLICATION\t*\n");
26 printf("********************************************************* ");
27
34 if( iN != iP)
35 {
36 printf("\nMatrix Multiplication not possible\n");
37 exit(0);
38 }
39
40
14
CHAPTER 6. MATRIX MULTIPLICATION
42 for(i=0;i<iM;i++)
43 for(j=0;j<iN;j++)
44 scanf("%d",&iaMat1[i][j]);
45
51
52 for(i=0;i<iM;i++)
53 {
54 for(j=0;j<iQ;j++)
55 {
56 for(k=0;k<iN;k++)
57 {
58 iaProd[i][j] += iaMat1[i][k] * iaMat2[k][j];
59 }
60 }
61 }
62
63 /*
*****************************************************************************************
64 |*| |*|
65 a00 a01 a02|*|b00 b01 b02|*|
66 |*| |*|
67 a10 a11 a12|*|b10 b11 b12|*|
68 |*| |*|
69 a20 a21 a22|*|b20 b21 b22|*|
70 |*| |*|
71
75 ********************************************************************************************
*/
76
77
78 printf("\nMatrix 1\n");
79
80 for(i=0;i<iM;i++)
81 {
82 for(j=0;j<iN;j++)
83 {
84 printf("%d\t",iaMat1[i][j]);
85 }
86 printf("\n");
87 }
88 printf("\n");
89
90 printf("\nMatrix 2\n");
91
92 for(i=0;i<iP;i++)
93 {
94 for(j=0;j<iQ;j++)
95 {
96 printf("%d\t",iaMat2[i][j]);
97 }
98 printf("\n");
99 }
100 printf("\n");
101
104 for(i=0;i<iM;i++)
105 {
106 for(j=0;j<iQ;j++)
107 {
108 printf("%d\t",iaProd[i][j]);
109 }
110 printf("\n");
111 }
112 printf("\n");
113 return 0;
114 }
Listing 6.1: A06MatrixMul.c
Output
=================================
1 /***************************************
2
3 *********************************************************
4 * PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
5 *********************************************************
6 Enter the order of Matrix1
7 2 3
8
14 *********************************************************
15 * PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
16 *********************************************************
17 Enter the order of Matrix1
18 2 3
19
32 Matrix 1
33 1 2 3
34 4 5 6
35
36 Matrix 2
37 1 2
38 3 4
39 5 6
40
44
45 *********************************************************
46 * PROGRAM TO IMPLEMENT MATRIX MULIPLICATION *
47 *********************************************************
48 Enter the order of Matrix1
49 2 2
50
62 Matrix 1
63 1 2
64 3 4
65
66 Matrix 2
67 1 0
68 0 1
69
Question
Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the built-in library function. Print both the
results with appropriate inferences.
C Code
1 /***************************************************************************
2 *File : A07SineCosAngle.c
3 *Description : Program to calculate Sin(x)/Cos(x) using Taylor series
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8 #include<stdio.h>
9 #include<stdlib.h>
10 #include <math.h>
11 /***************************************************************************
12 *Function : main
13 *Input parameters : no parameters
14 *RETURNS : 0 on success
15 *************************************************************************** /
16 int main()
17 {
26 fAngR= (fAngD*M_PI)/180 ;
27
18
CHAPTER 7. COMPUTE SINE AND COSINE OF AN ANGLE
57 return 0;
58 }
Listing 7.1: A07SineCosAngle.c
Output
=================================
1 /***************************************
2
Bubble Sort
Question
Sort the given set of N numbers using Bubble sort.
C Code
1 /***************************************************************************
2 *File : A08BubbleSort.c
3 * Description : Program to implement Bubble Sort Algorithm
4 * Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters : no parameters
15 *RETURNS : 0 on success
16 *************************************************************************** /
17
18 int main(void)
19 {
20 int iNum, i, j, iaArr[10], iTemp;
21
22
23 printf("\n*************************************************");
24 printf("\n*\tPROGRAM TO IMPLEMENT BUBBLE SORT\t*\n");
25 printf("************************************************* ");
26
27 printf("\nEnter no of elements\n");
28 scanf("%d",&iNum);
29
34 for(i=0;i<iNum;i++)
35 {
36 for(j=0;j<iNum-i-1;j++)
37 {
38 if(iaArr[j] > iaArr[j+1])
39 {
40 iTemp = iaArr[j];
41 iaArr[j] = iaArr[j+1];
20
CHAPTER 8. BUBBLE SORT
42 iaArr[j+1] = iTemp;
43 }
44
51 }
52
55 for(i=0;i<iNum;i++)
56 printf("%d\t",iaArr[i]);
57
58 printf("\n");
59 return 0;
60 }
Listing 8.1: A08BubbleSort.c
Output
=================================
1 /***************************************
2 *************************************************
3 * PROGRAM TO IMPLEMENT BUBBLE SORT *
4 *************************************************
5 Enter no of elements
6 5
7
14 *************************************************
15 * PROGRAM TO IMPLEMENT BUBBLE SORT *
16 *************************************************
17 Enter no of elements
18 6
19
String Operations
Question
Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing
techniques.
C Code
1 /***************************************************************************
2 *File : A09StringFunctions.c
3 *Description : Program to implement string operations as functions
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
16 /***************************************************************************
17 *Function : main
18 * Input parameters : no parameters
19 *RETURNS : 0 on success
20 *************************************************************************** /
21
22 int main()
23 {
24 int iChoice;
25 char acStr1[30], acStr2[30];
26 int iLen;
27 printf("\n=====================\n");
28 printf("STRING OPERATIONS");
29 printf("\n=====================\n");
30 for(;;)
31 {
32 printf("\nEnter two strings\n");
33 printf("\nString 1 : "); scanf("%s", acStr1);
34 printf("\nString 2 : "); scanf("%s", acStr2);
35 printf("\n1.String Compare\n2.String Concatenate\n3.String Length");
36 printf("\nEnter your choice : "); scanf("%d", &iChoice);
37 switch(iChoice)
38 {
39 case 1: if(fnMyStrCmp(acStr1, acStr2) == 0)
22
CHAPTER 9. STRING OPERATIONS
57 }
58 printf("\nPress 1 to continue and 0 to quit : ");
59 scanf("%d", &iChoice);
60 if(0==iChoice)
61 {
62 break;
63 }
64 }
65 return 0;
66 }
67
68 /***************************************************************************
69 *Function : fnMyStrCmp
70 *Description : Function that compares the two strings s1 and s2.
71 *Input parameters :
72 * const char * s1, const char *s2 - two strings to be compared
73 *RETURNS :
74 * 1 if s1 is greater than s2.
75 * 0 if s1 matches s2.
76 * -1 if s1 is less than s2.
77 *************************************************************************** /
78
98 /***************************************************************************
99 *Function : fnMyStrCat
100 *Description : function that appends the src string to the dest string
101 *Input parameters :
102 * char *dest - first string
103 * const char *src - second string
104 *RETURNS : nothing
105 *************************************************************************** /
106
116 /***************************************************************************
117 *Function : fnMyStrLen
118 *Description : function that calculates the length of a string
119 *Input parameters :
120 * const char * str - string whose length needs to be found
121 *RETURNS :
122 * integer which is the length of the string
123 *************************************************************************** /
124
Output
=================================
1 /***************************************
2 =====================
3 STRING OPERATIONS
4 =====================
5
8 String 1 : shiva
9
10 String 2 : shankar
11
12 1.String Compare
13 2.String Concatenate
14 3.String Length
15 Enter your choice : 2
16
17 Concatenated String is
18 shivashankar
19 Press 1 to continue and 0 to quit : 1
20
23 String 1 : ramesh
24
25 String 2 : sumesh
26
27 1.String Compare
28 2.String Concatenate
29 3.String Length
30 Enter your choice : 1
31
37 String 1 : sam
38
39 String 2 : samantha
40
41 1.String Compare
42 2.String Concatenate
43 3.String Length
44 Enter your choice : 3
45
C Structures
Question
Implement structures to read, write and compute average- marks and the students scoring above and below the average marks
for a class of N students.
C Code
1 /***************************************************************************
2 *File : A10StudentStructure.c
3 *Description : Program to implement structure and compute average marks
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 10 August 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11 #define STRSIZE 30
12
13 typedef struct
14 {
15 char cName[STRSIZE];
16 char cUSN[11];
17 int iMarks;
18 }STUDENT_TYPE;
19
20 /***************************************************************************
21 *Function : main
22 *Input parameters : no parameters
23 *RETURNS : 0 on success
24 *************************************************************************** /
25
26 int main(void)
27 {
28 STUDENT_TYPE students[100];
29 int iNum, i;
30 double dAvg = 0.0;
31
26
CHAPTER 10. C STRUCTURES
45 dAvg /= iNum;
46
49 for(i=0;i<iNum;i++)
50 {
51 printf("\n###############################");
52 printf("\nName\t: %s", students[i].cName);
53 printf("\nUSN\t: %s", students[i].cUSN);
54 printf("\nMarks\t: %d", students[i].iMarks);
55 if(students[i].iMarks < dAvg)
56 printf("\nThe student has scored below average\n");
57 else
58 printf("\nThe student has scored above average\n");
59 }
60
61 return 0;
62 }
Listing 10.1: A10StudentStructure.c
Output
=================================
1 /***************************************
2 Enter the number of students : 4
3 Enter the Student details
4
5 =========================================
6 Name : Raju
7 USN : 1SI17CS036
8 Marks : 67
9 =========================================
10 Name : Michael
11 USN : 1SI17CS045
12 Marks : 87
13 =========================================
14 Name : Sahana
15 USN : 1SI17CS405
16 Marks : 77
17 =========================================
18 Name : Jonathan
19 USN : 1SI17CS025
20 Marks : 83
21
24 =========================================
25 Name : Raju
26 USN : 1SI17CS036
27 Marks : 67
28 The student has scored below average
29 =========================================
30 Name : Michael
31 USN : 1SI17CS045
32 Marks : 87
33 The student has scored above average
34 =========================================
35 Name : Sahana
36 USN : 1SI17CS405
37 Marks : 77
38 The student has scored below average
39 =========================================
40 Name : Jonathan
41 USN : 1SI17CS025
42 Marks : 83
43 The student has scored above average
44 *************************************** /
Listing 10.2: OUTPUT
Question
Develop a program using pointers to compute the sum, mean and standard deviation of all ele- ments stored in an array of N
real numbers.
C Code
1 /***************************************************************************
2 *File : A11MeanVarianceSD.c
3 *Description : Program to compute Mean, Variance and Standard Deviation
4 using pointer to an array
5 *Author : Prabodh C P
6 *Compiler : gcc compiler, Ubuntu 22.04
7 *Date : 10 August 2022
8 *************************************************************************** /
9 #include<stdio.h>
10 #include<stdlib.h>
11 #include<math.h>
12 /***************************************************************************
13 *Function : main
14 *Input parameters : no parameters
15 *RETURNS : 0 on success
16 *************************************************************************** /
17 int main(void)
18 {
19 int i,iNum;
20 float fMean = 0.0f, fVariance = 0.0f, fSd = 0.0f,faArray[100],fSum=0.0f;
21 float *fptr;
22
29
CHAPTER 11. POINTERS AND ARRAYS
40 fVariance /= iNum;
41 fSd = sqrt(fVariance);
42 printf("\nThe values entered are");
43 for(i=0; i<iNum; i++)
44 {
45 printf("\t%g",fptr[i]); //printf("\n\t%f",*(fptr+i));
46 }
47 printf("\n");
48
49 printf("\n**************************************\n");
50 printf("\tSum\t = \t%g\n\tMean\t = \t%g\n\tVariance = \t%g\nStandard Deviation =
\t%g",fSum,fMean,fVariance,fSd);
51 printf("\n**************************************\n");
52 return 0;
53 }
Listing 11.1: A11MeanVarianceSD.c
Output
=================================
1 /***************************************
2 Enter the number of Values : 4
3
4 Enter 4 values
5 1.1 2.2 3.3 4.4
6
9 ******************************** ******
10 Sum = 11
11 Mean = 2.75
12 Variance = 1.5125
13 Standard Deviation = 1.22984
14 **************************************
15 ============================================================
17
18 Enter 5 values
19 5.345 6.765 7.234 8.675 9.765
20
23 **************************************
24 Sum = 37.784
25 Mean = 7.5568
26 Variance = 2.34995
27 Standard Deviation = 1.53295
28 **************************************
29 *************************************** /
Listing 11.2: out11.c
File Copy
Question
Write a C program to copy a text file to another, read both the input file name and target file name.
C Code
1 /***************************************************************************
2 *File : A12FileCopy.c
3 *Description : Program to copy a text file to another
4 *Author : Prabodh C P
5 *Compiler : gcc compiler, Ubuntu 22.04
6 *Date : 22 December 2022
7 *************************************************************************** /
8
9 #include<stdio.h>
10 #include<stdlib.h>
11
12 /***************************************************************************
13 *Function : main
14 *Input parameters : no parameters
15 *RETURNS : 0 on success
16 *************************************************************************** /
17 int main(void)
18 {
19 FILE *fp1,*fp2;
20 int ch;
21 char fname1[100], fname2[100];
22 printf("\nEnter File name to be copied\n");
23 scanf("%s",fname1);
24 fp1 = fopen(fname1,"r");
25
26 if(fp1 == NULL)
27 {
28 printf("\nInput File %s doesn’t exist\n", fname1);
29 exit(0);
30 }
31
35 fp2 = fopen(fname2,"w");
36 while((ch=fgetc(fp1)) != EOF)
37 {
38 fputc(ch,fp2);
39 }
40 printf("\nFile %s successfully created\n",fname2);
31
CHAPTER 12. FILE COPY
41
42 fclose(fp1);
43 fclose(fp2);
44
45 return 0;
46 }
Listing 12.1: A12FileCopy.c
Output
=================================
1 /**************************************************
2 Enter File name to be copied
3 out9.c
4
10 ===============================================
11
17 **************************************************/
Listing 12.2: out12.c