Introduction to C Programming Lab Manual for VTU (BESCK104E/204E)

In this blog post, you will find solutions for the Introduction to C Programming Lab Manual (BESCK104E/204E) course work for the I year students of VTU university. The academic syllabus has been changed for the year 2022-23 in VTU. A new subject has been introduced titled “Introduction to C Programming” (Sub Code: BESCK104E/204E) for first year students of VTU. This subject has an integrated lab component. Anybody can contribute and re-share this manual as it is distributed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Also note the same subject is part of the autonomous curriculum at Siddaganga Institute of technology with code ESCO5.

All the programs have been compiled using the GNU GCC compiler version 9.4.0 and has been tested on Ubuntu 22.04.4 LTS (Jammy Jellyfish)  64-bit Kernel Linux 5.15.0-56-generic.

These programs can run on any GNU/Linux Operating system or any other OS with GCC installed.

GCC compiler can be obtained from here. A nice tutorial is provided here to get started with GCC.

If you find the command line difficult to handle and looking for an GUI based IDE to work with, the best choice is Code::Blocks. Code::Blocks uses the GNU GCC compiler collection. Two other good IDEs that you can try or Eclipse and Anjuta. I have provided links to this IDEs below you can click and download the necessary software and instructions to set up a working environment.

  1. Code::Blocks
  2. Anjuta DevStudio
  3. Eclipse IDE for C/C++

After getting the necessary development environment setup, Now lets focus on the solutions. You can go to the appropriate program by clicking on the links below.

  1. Question 01 – Mechanical Energy of a Particle
  2. Question 02 – Distance Conversion
  3. Question 03 – Check Character case
  4. Question 04 – Balancing Chemical Equation
  5. Question 05 – Matrix Multiplication
  6. Question 06 – Compute Sine and Cosine of an Angle
  7. Question 07 – Bubble Sort
  8. Question 08 – String Operations
  9. Question 09 – C Structures
  10. Question 10 – Pointers and Arrays

Question 1

Mechanical Energy of a Particle

Write a C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.

C Code

/***************************************************************************
*File			: A01MechEnergy.c
*Description	: C Program to find Mechanical Energy of a particle using E = mgh+1/2 mv2.
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 02 December 2022
***************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

const double ACCL_GRAV = 9.806;

/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/

int main()
{
	double dMass, dHeight, dVelocity;
	double dPotEng, dKinEng, dEng;

	printf("\n*************************************************************");
	printf("\n*\tProgram to find Mechanical Energy of a body\t    *\n");
	printf("*************************************************************");

	
	printf("\nEnter the mass (in kg) of the body: "); scanf("%lf", &dMass);
	printf("\nEnter the height (in metres) of the body: "); scanf("%lf", &dHeight);
	printf("\nEnter the velocity (in meters per second) of the body: "); scanf("%lf", &dVelocity);

	dPotEng = dMass * ACCL_GRAV * dHeight;
	dKinEng = dMass * dVelocity * dVelocity	/ 2;
	dEng = dPotEng + dKinEng;
	
	printf("\nPotential energy associated with the body is %0.3lf Joules\n", dPotEng);
	printf("\nKinetic energy associated with the body is %0.3lf Joules\n", dKinEng);
	printf("\nTotal energy associated with the body is %0.3lf Joules\n", dEng);
	
	return 0;		
}

Output

*************************************************************
*	Program to find Mechanical Energy of a body	    *
*************************************************************
Enter the mass (in kg) of the body: 80   

Enter the height (in metres) of the body: 10

Enter the velocity (in meters per second) of the body: 10

Potential energy associated with the body is 7844.800 Joules

Kinetic energy associated with the body is 4000.000 Joules

Total energy associated with the body is 11844.800 Joules

*************************************************************


Question 2

Distance Conversion

Develop a C Program to convert Kilometers into Meters and Centimeters.

C Code

/***************************************************************************
*File			: A02DistConvert.c
*Description	: Program to convert Kilometers into Meters and Centimeters.
*Author			: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 02 December 2022
***************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/
int main(void)
{
	double dDistKm, dDistMtr, dDistCm;


	printf("n*************************************************************");
	printf("n*tProgram to convert Kilometers into Meters and Centimeterst    *n");
	printf("*************************************************************");

	printf("nEnter the distance in kilometers : ");	scanf("%lf",&dDistKm);

	dDistMtr = dDistKm * 1000;
	dDistCm = dDistMtr * 100;
	
	printf("nThe distance entered in kilometers is : %0.3lf n", dDistKm);
	printf("nEquivalent distance in meters is : %0.3lf n", dDistMtr);
	printf("nEquivalent distance in centimeters is : %0.3lf n", dDistCm);
	return 0;
}

Output

***********************************************************************
*	Program to convert Kilometers into Meters and Centimeters	    *
***********************************************************************
Enter the distance in kilometers : 63

The distance entered in kilometers is : 63.000 

Equivalent distance in meters is : 63000.000 

Equivalent distance in centimeters is : 6300000.000 

***********************************************************************


Question 3

Check Character case

Write a C program To Check the Given Character is Lowercase or Uppercase or Special Character.

C Code

/***************************************************************************
*File			: A03CheckCharacter.c
*Description	: Program to check the given character is Lowercase or Uppercase or Special character.
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
/***************************************************************************
*Function		: 	main
*Input parameters	:	no parameters
*RETURNS		:	0 on success
***************************************************************************/
int main(void)
{
	char cChar;
	printf("nEnter a character to be checked : "); 	scanf("%c", &cChar);
	
	if(cChar >= 'a' && cChar <= 'z')
	{
		printf("nThe character entered is a lower case charactern");
	}
	else if(cChar >= 'A' && cChar <= 'Z')
	{
		printf("nThe character entered is a upper case charactern");
	}
	else if(cChar >= '0' && cChar <= '9')
	{
		printf("nThe character entered is a digitn");
	}
	else
	{
		printf("nThe character entered is a special charactern");
	}

	return 0;
}

Output

***************************************
Enter a character to be checked : 1

The character entered is a digit

Enter a character to be checked : #

The character entered is a special character

Enter a character to be checked : s

The character entered is a lower case character

Enter a character to be checked : S

The character entered is a upper case character
***************************************

Question 4

Balancing Chemical Equation

Write a C program to balance the given Chemical Equation values x, y, p, q of a simple chemical equation of the type: The task is to find the values of constants b1, b2, b3 such that the equation is balanced on both sides and it must be the reduced form.

Generic Chemical Equation Form b1 ∗ Ax + b2 ∗ By ⇒ b3 (Ap Bq)

C Code

/***************************************************************************
*File			: A04BalanceChemEqn.c
*Description	: Program to balance the given Chemical Equation 
					of the form  b1*Ax + b2*By ==> b3(ApBq)
*Author			: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 26 December 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>

int fnGCD(int, int );

/***************************************************************************
*Function		: 	main
*Input parameters	:	no parameters
*RETURNS		:	0 on success
***************************************************************************/
int main(void)
{
	int x, y, p, q;
	int b1, b2, b3;
	int iCommDivisor;

	printf("Enter the atomocity(x) of Element1 : "); 	scanf("%d", &x);
	printf("Enter the atomocity(y) of Element2 : "); 	scanf("%d", &y);
	printf("Enter the atomocity(p) of Element1 in the compound : "); 	scanf("%d", &p);	
	printf("Enter the atomocity(q) of Element2 in the compound : "); 	scanf("%d", &q);	
		
	b1 = p * y;
	b2 = q * x;
	b3 = x * y;

	//if b1, b2 and b3 together have a greatest common divisor divide each one by that greatest common divisor 
	iCommDivisor = fnGCD(b1,b2);
	iCommDivisor = fnGCD(b3, iCommDivisor);

	b1 = b1 / iCommDivisor;
	b2 = b2 / iCommDivisor;
	b3 = b3 / iCommDivisor;

	printf("nx = %dty = %dtp = %dtq = %dn", x, y, p, q);
	printf("nb1 = %dtb2 = %dtb3 = %dn", b1, b2,b3);
	printf("nBalanced Equation is now :nt%d*%d + %d*%d ==> %d(%d,%d)n", b1,x,b2,y,b3,p,q);
	
	return 0;
		
}
/***************************************************************************
*Function		: 	fnGCD
*Description	:	function to calculate GCD of two numbers
*Input parameters	:	iVal1 - non-negative integer
						iVal2 - non-negative integer
*RETURNS		:	greatest common divisor of iVal1 and iVal2
***************************************************************************/
int fnGCD(int iVal1, int iVal2)
{
	if (0 == iVal2)
		return iVal1;
	return fnGCD(iVal2, iVal1 % iVal2);
}

Output

***************************************

Enter the atomocity(x) of Element1 : 2
Enter the atomocity(y) of Element2 : 2
Enter the atomocity(p) of Element1 in the compound : 2
Enter the atomocity(q) of Element2 in the compound : 1

x = 2	y = 2	p = 2	q = 1

b1 = 2	b2 = 1	b3 = 2

Balanced Equation is now :
	2*2 + 1*2 ==> 2(2,1)


Enter the atomocity(x) of Element1 : 2
Enter the atomocity(y) of Element2 : 3
Enter the atomocity(p) of Element1 in the compound : 4
Enter the atomocity(q) of Element2 in the compound : 5

x = 2	y = 3	p = 4	q = 5

b1 = 6	b2 = 5	b3 = 3

Balanced Equation is now :
	6*2 + 5*3 ==> 3(4,5)

***************************************


Question 5

Matrix Multiplication

Write a C program to implement Matrix multiplication and validate the rules of multiplication.

C Code

/***************************************************************************
*File			: A05MatrixMul.c
*Description	: Program to implement Matrix Multiplication
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/

#include<stdio.h>
#include<stdlib.h>

/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/

int main(void)
{
	int iM, iN, iP, iQ, i, j, k, iaMat1[10][10], iaMat2[10][10];
	int iaProd[10][10] = {0};


	printf("n*********************************************************");
	printf("n*tPROGRAM TO IMPLEMENT MATRIX MULIPLICATIONt*n");
	printf("*********************************************************");

	printf("nEnter the order of Matrix1n");
	scanf("%d%d",&iM,&iN);

	printf("nEnter the order of Matrix2n");
	scanf("%d%d",&iP,&iQ);

	if( iN != iP)
	{
		printf("nMatrix Multiplication not possiblen");
		exit(0);
	}


	printf("nEnter the elements of Matrix 1n");
	for(i=0;i<iM;i++)
		for(j=0;j<iN;j++)
			scanf("%d",&iaMat1[i][j]);

	printf("nEnter the elements of Matrix 2n");
	for(i=0;i<iP;i++)
		for(j=0;j<iQ;j++)
			scanf("%d",&iaMat2[i][j]);


	for(i=0;i<iM;i++)
	{
		for(j=0;j<iQ;j++)
		{
			for(k=0;k<iN;k++)
			{
				iaProd[i][j] += iaMat1[i][k] * iaMat2[k][j];
			}
		}
	}

/**************************************************************************************************
	         |*|	         |*|
a00  a01  a02|*|b00  b01  b02|*|
	         |*|	         |*|
a10  a11  a12|*|b10  b11  b12|*|
	         |*|	         |*|
a20  a21  a22|*|b20  b21  b22|*|
	         |*|	         |*|

(a00*b00+a01*b10+a02*b20) (a00*b01+a01*b11+a02*b21) (a00*b02+a01*b12+a02*b22)
(a10*b00+a11*b10+a12*b20) (a10*b01+a11*b11+a12*b21) (a10*b02+a11*b12+a12*b22)
(a20*b00+a21*b10+a22*b20) (a20*b01+a21*b11+a22*b21) (a20*b02+a21*b12+a22*b22)
**************************************************************************************************/


	printf("nMatrix 1n");

	for(i=0;i<iM;i++)
	{
		for(j=0;j<iN;j++)
		{
			printf("%dt",iaMat1[i][j]);
		}
		printf("n");
	}
	printf("n");

	printf("nMatrix 2n");

	for(i=0;i<iP;i++)
	{
		for(j=0;j<iQ;j++)
		{
			printf("%dt",iaMat2[i][j]);
		}
		printf("n");
	}
	printf("n");

	printf("nThe Product matrix is is n");

	for(i=0;i<iM;i++)
	{
		for(j=0;j<iQ;j++)
		{
			printf("%dt",iaProd[i][j]);
		}
		printf("n");
	}
	printf("n");
	return 0;
}

Output

*********************************************************
*	PROGRAM TO IMPLEMENT MATRIX MULIPLICATION	*
*********************************************************
Enter the order of Matrix1
2 3

Enter the order of Matrix2
4 5

Matrix Multiplication not possible

*********************************************************
*	PROGRAM TO IMPLEMENT MATRIX MULIPLICATION	*
*********************************************************
Enter the order of Matrix1
2 3

Enter the order of Matrix2
3 2

Enter the elements of Matrix 1
1 2 3
4 5 6

Enter the elements of Matrix 2
1 2
3 4
5 6

Matrix 1
1	2	3	
4	5	6	

Matrix 2
1	2	
3	4	
5	6	

The Product matrix is is 
22	28	
49	64	

*********************************************************
*	PROGRAM TO IMPLEMENT MATRIX MULIPLICATION	*
*********************************************************
Enter the order of Matrix1
2 2

Enter the order of Matrix2
2 2

Enter the elements of Matrix 1
1 2 
3 4

Enter the elements of Matrix 2
1 0
0 1

Matrix 1
1	2	
3	4	

Matrix 2
1	0	
0	1	

The Product matrix is is 
1	2	
3	4	
***************************************


Question 6

Compute Sine and Cosine of an Angle

Write a C program to 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

/***************************************************************************
*File			: A06SineCosAngle.c
*Description	: Program to calculate Sin(x)/Cos(x) using Taylor series
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/
int main()
{
	float fAngD, fAngR;
	float fTerm, fNum, fDen, fSVal,fCVal;
	int i,iNum;
	printf("nEnter the Angle : ");	scanf("%f",&fAngD);
	printf("nEnter the Number of terms : ");		scanf("%d",&iNum);
	printf("nInput Angle = %gn",fAngD);	
	printf("No of terms = %dn",iNum);

	fAngR= (fAngD*M_PI)/180 ;
	
	//Calculation of Sine of an angle using Taylor's series
	fNum=fAngR;
	fDen=1.0;
	fSVal =0.0;
	fTerm=fNum/fDen;
	for(i=1;i<=iNum;i++)
	{
		fSVal = fSVal + fTerm;
		fNum = -fNum * fAngR * fAngR ;
		fDen = fDen * (2*i) * (2*i+1);
		fTerm = fNum/fDen;
	}

	//Calculation of Cosine of an angle using Taylor's series
	fNum=1.0;
	fDen=1.0;
	fCVal =0.0;
	fTerm=1.0;
	for(i=1;i<=iNum;i++)
	{
		fCVal = fCVal + fTerm;
		fNum = -fNum * fAngR * fAngR ;
		fDen = fDen * (2*i) * (2*i-1);
		fTerm = fNum/fDen;
	}
	
	printf("nCalculated value is :nSin(%g)/Cos(%g) = %gn",fAngD, fAngD, fSVal/fCVal);
	printf("nBuilt In function value is :nSin(%g)/Cos(%g) = %gn", fAngD, fAngD, sin(fAngR)/cos(fAngR));

	return 0;
}

Output

***************************************

Enter the Angle : 60
Enter the Number of terms : 12

Input Angle = 60	No of terms = 12
Calculated value is :
Sin(60)/Cos(60) = 1.73205

Built In function value is :
Sin(60)/Cos(60) = 1.73205
=========================================

Enter the Angle : 30
Enter the Number of terms : 3

Input Angle = 30	No of terms = 3
Calculated value is :
Sin(30)/Cos(30) = 0.577334

Built In function value is :
Sin(30)/Cos(30) = 0.57735
=========================================

Enter the Angle : 45
Enter the Number of terms : 11

Input Angle = 45	No of terms = 11
Calculated value is :
Sin(45)/Cos(45) = 1

Built In function value is :
Sin(45)/Cos(45) = 1
***************************************

Question 7

Bubble Sort

Write a C program to sort the given set of N numbers using Bubble sort.

C Code

/***************************************************************************
*File			: A07BubbleSort.c
*Description	: Program to implement Bubble Sort Algorithm
*Author			: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/

#include<stdio.h>
#include<stdlib.h>

/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/

int main(void)
{
	int iNum, i, j, iaArr[10], iTemp;


	printf("n*************************************************");
	printf("n*tPROGRAM TO IMPLEMENT BUBBLE SORTt*n");
	printf("*************************************************");

	printf("nEnter no of elementsn");
	scanf("%d",&iNum);

	printf("nEnter the elementsn");
	for(i=0;i<iNum;i++)
		scanf("%d",&iaArr[i]);

	for(i=0;i<iNum;i++)
	{
		for(j=0;j<iNum-i-1;j++)
		{
			if(iaArr[j] > iaArr[j+1])
			{
				iTemp = iaArr[j];
				iaArr[j] = iaArr[j+1];
				iaArr[j+1] = iTemp;
			}

/*Code to show the program trace*/		
/*			printf("nIteration i=%d, j=%dn",i,j);*/
/*			for(k=0;k<iNum;k++)*/
/*				printf("%dt",iaArr[k]);*/
		}

	}

	printf("nThe Sorted array is n");

	for(i=0;i<iNum;i++)
		printf("%dt",iaArr[i]);

	printf("n");
	return 0;
}

Output

*************************************************
*	PROGRAM TO IMPLEMENT BUBBLE SORT	*
*************************************************
Enter no of elements
5

Enter the elements
2 1 6 5 7

The Sorted array is 
1	2	5	6	7	

*************************************************
*	PROGRAM TO IMPLEMENT BUBBLE SORT	*
*************************************************
Enter no of elements
6

Enter the elements
9 7 5 3 1 0

The Sorted array is 
0	1	3	5	7	9	
***************************************

Question 8

String Operations

Write functions to implement string operations such as compare, concatenate, string length. Convince the parameter passing techniques.

C Code

/***************************************************************************
*File			: A08StringFunctions.c
*Description	: Program to implement string operations as functions
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/

#include<stdio.h>
#include<stdlib.h>

int fnMyStrCmp(const char*, const char*);
void fnMyStrCat(char*, const char*);
int fnMyStrLen(const char*);

/***************************************************************************
*Function		: 	main
*Input parameters	:	no parameters
*RETURNS		:	0 on success
***************************************************************************/

int main()
{
	int iChoice;
	char acStr1[30], acStr2[30];
	int iLen;
	printf("n=====================n");
	printf("STRING OPERATIONS");
	printf("n=====================n");
	for(;;)
	{
		printf("nEnter two stringsn");
		printf("nString 1 : "); 	scanf("%s", acStr1);
		printf("nString 2 : "); 	scanf("%s", acStr2);
		printf("n1.String Comparen2.String Concatenaten3.String Length");
		printf("nEnter your choice : ");	scanf("%d", &iChoice);	
		switch(iChoice)
		{
			case 1: if(fnMyStrCmp(acStr1, acStr2) == 0)
						printf("nTwo strings are equal");
					else if(fnMyStrCmp(acStr1, acStr2) > 0)
						printf("nString %s is greater than String %s", acStr1, acStr2);
					else
						printf("nString %s is greater than String %s", acStr2, acStr1);
					break;
					
			case 2:	fnMyStrCat(acStr1, acStr2);
					printf("nConcatenated String isn%s", acStr1);
					break;
			
			case 3:	iLen = fnMyStrLen(acStr1);
					printf("nLength of String %s is %dn", acStr1, iLen);
					iLen = fnMyStrLen(acStr2);
					printf("nLength of String %s is %dn", acStr2, iLen);
					break;
					
		}
		printf("nPress 1 to continue and 0 to quit : ");
		scanf("%d", &iChoice);
		if(0==iChoice)
		{
			break;
		}
	}
	return 0;		
}

/***************************************************************************
*Function			: fnMyStrCmp
*Description		: Function that compares the two strings s1 and s2.  
*Input parameters	:
*		const char *s1, const char *s2 - two strings to be compared
*RETURNS		:
*	1 if s1 is greater than s2.
*	0 if s1 matches s2.
*	-1 if s1 is less than s2.
***************************************************************************/

int fnMyStrCmp(const char *s1, const char *s2)
{
	int k;
	for(k=0; s1[k] == s2[k] && s1[k]!=''&& s2[k]!=''; k++);

	if( k==(fnMyStrLen(s1)) && k==(fnMyStrLen(s2)) )	
	{
		return 0;
	}
	else if(s1[k] > s2[k])		
	{
		return 1;
	}
	else 	
	{
		return -1;
	}	
}

/***************************************************************************
*Function			: fnMyStrCat
*Description		: function that appends the src string to the dest string
*Input parameters	:
*		char *dest - first string 
*		const char *src - second string
*RETURNS			: nothing
***************************************************************************/

void fnMyStrCat(char *dest, const char *src)
{
	int dest_len, i;
	dest_len = fnMyStrLen(dest);
	for (i = 0 ; src[i] != '' ; i++)
		dest[dest_len + i] = src[i];
	dest[dest_len + i] = '';
}

/***************************************************************************
*Function			: fnMyStrLen
*Description		: function that calculates the length of a string
*Input parameters	:
*		const char *str - string whose length needs to be found
*RETURNS			: 
*	integer which is the length of the string
***************************************************************************/

int fnMyStrLen(const char *str)
{
	int iLen;
	for(iLen=0; str[iLen] != ''; iLen++);
	return iLen;
}

Output

***************************************
=====================
STRING OPERATIONS
=====================

Enter two strings

String 1 : shiva 

String 2 : shankar

1.String Compare
2.String Concatenate
3.String Length
Enter your choice : 2

Concatenated String is
shivashankar
Press 1 to continue and 0 to quit : 1

Enter two strings

String 1 : ramesh

String 2 : sumesh

1.String Compare
2.String Concatenate
3.String Length
Enter your choice : 1

String sumesh is greater than String ramesh
Press 1 to continue and 0 to quit : 1

Enter two strings

String 1 : sam

String 2 : samantha

1.String Compare
2.String Concatenate
3.String Length
Enter your choice : 3

Length of String sam is 3

Length of String samantha is 8

Press 1 to continue and 0 to quit : 0
***************************************

Question 9

C Structures

Write a C program to 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

/***************************************************************************
*File			: A09StudentStructure.c
*Description	: Program to implement structure and compute average marks
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#define STRSIZE 30

typedef struct
{
	char cName[STRSIZE];
	char cUSN[11];
	int iMarks;
}STUDENT_TYPE;

/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/

int main(void)
{
	STUDENT_TYPE students[100];
	int iNum, i;
	double dAvg = 0.0;
	
	printf("nEnter the number of students : ");
	scanf("%d", &iNum);
	
	printf("nEnter the Student detailsn");
	for(i=0;i<iNum;i++)
	{
		printf("n###############################");
		printf("nName : "); 	scanf("%s", students[i].cName);
		printf("nUSN : "); 	scanf("%s", students[i].cUSN);
		printf("nMarks : "); 	scanf("%d", &students[i].iMarks);
		dAvg += students[i].iMarks;
	}
	
	dAvg /= iNum;
	
	printf("nThe average marks for the class is : %gn", dAvg);
	
	for(i=0;i<iNum;i++)
	{
		printf("n###############################");		
		printf("nNamet: %s", students[i].cName);
		printf("nUSNt: %s", students[i].cUSN);
		printf("nMarkst: %d", students[i].iMarks);
		if(students[i].iMarks < dAvg)
			printf("nThe student has scored below averagen");
		else
			printf("nThe student has scored above averagen");
	}
		
	return 0;
}

Output

***************************************
Enter the number of students : 4
Enter the Student details

=========================================
Name : Raju
USN : 1SI17CS036
Marks : 67
=========================================
Name : Michael
USN : 1SI17CS045
Marks : 87
=========================================
Name : Sahana
USN : 1SI17CS405
Marks : 77
=========================================
Name : Jonathan
USN : 1SI17CS025
Marks : 83

The average marks for the class is : 78.5

=========================================
Name	: Raju
USN	: 1SI17CS036
Marks	: 67
The student has scored below average
=========================================
Name	: Michael
USN	: 1SI17CS045
Marks	: 87
The student has scored above average
=========================================
Name	: Sahana
USN	: 1SI17CS405
Marks	: 77
The student has scored below average
=========================================
Name	: Jonathan
USN	: 1SI17CS025
Marks	: 83
The student has scored above average
***************************************

Question 10

Pointers and Arrays

Develop a C program using pointers to compute the sum,mean and standard deviation of all elements stored in an array of N real numbers.

C Code

/***************************************************************************
*File			: A10MeanVarianceSD.c
*Description	: Program to compute Mean, Variance and Standard Deviation
					using pointer to an array
*Author		: Prabodh C P
*Compiler		: gcc compiler, Ubuntu 22.04
*Date			: 10 August 2022
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/***************************************************************************
*Function			: 	main
*Input parameters	:	no parameters
*RETURNS			:	0 on success
***************************************************************************/
int main(void)
{
    int i,iNum;
    float fMean = 0.0f, fVariance = 0.0f, fSd = 0.0f,faArray[100],fSum=0.0f;
    float *fptr;

    printf("nEnter the number of Values : ");
    scanf("%d",&iNum);
    fptr = faArray; 
/*    fptr = (float*)malloc(iNum*sizeof(float));*/
    printf("nEnter %d valuesn", iNum);
    for(i=0; i<iNum; i++)
    {
        scanf("%f",fptr+i);
        fSum += *(fptr+i);		//fSum += fptr[i]; this is also valid
    }
    fMean = fSum/iNum;

    for(i=0; i<iNum; i++)
    {
        fVariance += (fptr[i] - fMean)*(fptr[i] - fMean);
		//fVariance += (*(fptr+i) - fMean)*(*(fptr+i) - fMean);
    }
    fVariance /= iNum;
    fSd = sqrt(fVariance);
    printf("nThe values entered are");
    for(i=0; i<iNum; i++)
    {
        printf("t%g",fptr[i]);        //printf("nt%f",*(fptr+i));
    }
    printf("n");

    printf("n**************************************n");
    printf("tSumt = t%gntMeant = t%gntVariance = t%gnStandard Deviation = t%g",fSum,fMean,fVariance,fSd);
    printf("n**************************************n");
    return 0;
}

Output

***************************************
Enter the number of Values : 4

Enter 4 values
1.1 2.2 3.3 4.4

The values entered are	1.1	2.2	3.3	4.4

**************************************
	Sum	 = 	11
	Mean	 = 	2.75
	Variance = 	1.5125
Standard Deviation = 	1.22984
**************************************
============================================================
Enter the number of Values : 5

Enter 5 values
5.345 6.765 7.234 8.675 9.765

The values entered are	5.345	6.765	7.234	8.675	9.765

**************************************
	Sum	 = 	37.784
	Mean	 = 	7.5568
	Variance = 	2.34995
Standard Deviation = 	1.53295
**************************************

I am excited about the endless possibilities this subject and its lab manual offer. So, let’s come together, share our knowledge, and empower ourselves and others with the magic of C programming. The journey begins now, and the sky’s the limit!

Stay tuned for updates and new contributions. Happy coding, everyone!  Looking for your feedback. Report if there are any errors in the manual in the comments section.

For other programming lab manuals of First year can be found in this blog or by clicking on the button below.

Prabodh C P is a faculty in the Dept of CSE SIT, Tumkur and also currently a Research Scholar pursuing PhD in IIT Hyderabad. He conducts online classes for C, C++, Python. For more info call +919392302100

Leave a Reply

Your email address will not be published. Required fields are marked *