Annotated-C Assignment 202017b3723
Annotated-C Assignment 202017b3723
Id- 202017B3723
Section 1 ( Strings )
Q2. A paragraph consists of multiple lines. Write a program to count the number of articles in it.
#include<stdio.h>
#include<string.h>
int main()
{
char para[300];
printf("Enter a paragraph: ");
gets(para);
int count = 0;
char *token = strtok(para, " ");
while(token != NULL)
{
if(strcmpi(token, "a") == 0 || strcmpi(token, "an") == 0 || strcmpi(token, "the") == 0)
count++;
-----------------------------------------------------------------------------------------------------------------------------------
---
Q3. Output: 4 3. Assume that there are two paragraphs. Write a program to count the number of
lines, number of words and number of characters in each of it.
#include<stdio.h>
#include<string.h>
int main()
{
char para1[500],para2[500],temp;
int k=0;
printf("Enter a paragraph 1 (Press ; to end paragraph): ");
while((temp = getchar()) != ';'){
para1[k] = temp;
k++;
}
para1[k] = '\0';
k=0;
printf("\nEnter a paragraph 2 (Press ; to end paragraph): ");
while((temp = getchar()) != ';'){
para2[k] = temp;
k++;
}
para2[k] = '\0';
//counting number of lines,number of words,number of characters in paragraph 2
int lines1=0,words1=0,chars1=0;
for(int i=0;para1[i]!='\0';i++)
{
if(para1[i]=='\n')
lines1++;
if(para1[i]==' ' && para1[i+1]!=' ')
words1++;
if(para1[i]!=' ' && para1[i]!='\n')
chars1++;
}
printf("\nNumber of lines in paragraph 1: %d",lines1);
printf("\nNumber of words in paragraph 1: %d",words1);
printf("\nNumber of characters in paragraph 1: %d\n",chars1);
int lines2=0,words2=0,chars2=0;
for(int i=0;para2[i]!='\0';i++)
{
if(para2[i]=='\n')
lines2++;
if(para2[i]==' ' && para2[i+1]!=' ')
words2++;
if(para2[i]!=' ' && para2[i]!='\n')
chars2++;
}
printf("\nNumber of lines in paragraph 2: %d",lines2);
printf("\nNumber of words in paragraph 2: %d",words2);
printf("\nNumber of characters in paragraph 2: %d\n",chars2);
}
-----------------------------------------------------------------------------------------------------------------------------------
---
int main()
{
int arr[]={55, 23, 45, 12, 67, 20 , 34, 10 , 54, 50, 19};
int size = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, size);
}
-----------------------------------------------------------------------------------------------------------------------------------
---
void initializeEmployee(){
//intializing the structure with some values
strcpy(emp[0].empName,"Employee 1");
emp[0].empNumber = 1;
strcpy(emp[0].empDepartment,"IT");
strcpy(emp[0].empDesignation,"Developer");
emp[0].empSalary = 412.00;
strcpy(emp[1].empName,"Employee 2");
emp[1].empNumber = 2;
strcpy(emp[1].empDepartment,"HR");
strcpy(emp[1].empDesignation,"Manager");
emp[1].empSalary = 367.00;
strcpy(emp[2].empName,"Employee 3");
emp[2].empNumber = 3;
strcpy(emp[2].empDepartment,"Finance");
strcpy(emp[2].empDesignation,"Accountant");
emp[2].empSalary = 235.00;
strcpy(emp[3].empName,"Employee 4");
emp[3].empNumber = 4;
strcpy(emp[3].empDepartment,"HR");
strcpy(emp[3].empDesignation,"Manager");
emp[3].empSalary = 567.00;
strcpy(emp[4].empName,"Employee 5");
emp[4].empNumber = 5;
strcpy(emp[4].empDepartment,"Finance");
strcpy(emp[4].empDesignation,"Accountant");
emp[4].empSalary = 678.00;
}
void SortEmployee(){
//sort based on department
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(strcmp(emp[i].empDepartment,emp[j].empDepartment)<0){
struct employee temp;
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
int main()
{
initializeEmployee();
SortEmployee();
printEmployee();
return 0;
}
Q11. Assume that there are ‘n’ employee records. Each employee record consists of employee
name, employee number, department, designation and salary. Write a program to
a) Find the average salary of the employees
#include <stdio.h>
#include <string.h>
//declaring employee structure
struct employee
{
char empName[20];
int empNumber;
char empDepartment[20];
char empDesignation[20];
float empSalary;
}emp[5];
void initializeEmployee(){
//intializing the structure with some values
strcpy(emp[0].empName,"Employee 1");
emp[0].empNumber = 1;
strcpy(emp[0].empDepartment,"IT");
strcpy(emp[0].empDesignation,"Developer");
emp[0].empSalary = 412.00;
strcpy(emp[1].empName,"Employee 2");
emp[1].empNumber = 2;
strcpy(emp[1].empDepartment,"HR");
strcpy(emp[1].empDesignation,"Manager");
emp[1].empSalary = 367.00;
strcpy(emp[2].empName,"Employee 3");
emp[2].empNumber = 3;
strcpy(emp[2].empDepartment,"Finance");
strcpy(emp[2].empDesignation,"Accountant");
emp[2].empSalary = 235.00;
strcpy(emp[3].empName,"Employee 4");
emp[3].empNumber = 4;
strcpy(emp[3].empDepartment,"HR");
strcpy(emp[3].empDesignation,"Manager");
emp[3].empSalary = 567.00;
strcpy(emp[4].empName,"Employee 5");
emp[4].empNumber = 5;
strcpy(emp[4].empDepartment,"Finance");
strcpy(emp[4].empDesignation,"Accountant");
emp[4].empSalary = 678.00;
}
void findAverageSalary(){
int i;
float sum = 0,averageSalary;
//calculating the average salary
for(i=0;i<5;i++){
sum = sum + emp[i].empSalary;
}
averageSalary = sum/5;
printf("Average Salary of the employees: %f\n",averageSalary);
}
int main()
{
initializeEmployee();
findAverageSalary();
return 0;
}
b) Display all the employees who are getting more than average salary
#include <stdio.h>
#include <string.h>
//declaring employee structure
struct employee
{
char empName[20];
int empNumber;
char empDepartment[20];
char empDesignation[20];
float empSalary;
}emp[5];
void initializeEmployee(){
//intializing the structure with some values
strcpy(emp[0].empName,"Employee 1");
emp[0].empNumber = 1;
strcpy(emp[0].empDepartment,"IT");
strcpy(emp[0].empDesignation,"Developer");
emp[0].empSalary = 412.00;
strcpy(emp[1].empName,"Employee 2");
emp[1].empNumber = 2;
strcpy(emp[1].empDepartment,"HR");
strcpy(emp[1].empDesignation,"Manager");
emp[1].empSalary = 367.00;
strcpy(emp[2].empName,"Employee 3");
emp[2].empNumber = 3;
strcpy(emp[2].empDepartment,"Finance");
strcpy(emp[2].empDesignation,"Accountant");
emp[2].empSalary = 235.00;
strcpy(emp[3].empName,"Employee 4");
emp[3].empNumber = 4;
strcpy(emp[3].empDepartment,"HR");
strcpy(emp[3].empDesignation,"Manager");
emp[3].empSalary = 567.00;
strcpy(emp[4].empName,"Employee 5");
emp[4].empNumber = 5;
strcpy(emp[4].empDepartment,"Finance");
strcpy(emp[4].empDesignation,"Accountant");
emp[4].empSalary = 678.00;
}
void displayEmployeeGettingMoreThanAverageSalary(){
int i;
float sum = 0,averageSalary;
//calculating the average salary
for(i=0;i<5;i++){
sum = sum + emp[i].empSalary;
}
averageSalary = sum/5;
printf("\nAverage Salary is: %.2f\n",averageSalary);
printf("\nEmployees getting more than average salary: \n");
for(i=0;i<5;i++){
if(emp[i].empSalary > averageSalary){
printf("\nEmployee Name: %s\n",emp[i].empName);
printf("Employee Number: %d\n",emp[i].empNumber);
printf("Employee Department: %s\n",emp[i].empDepartment);
printf("Employee Designation: %s\n",emp[i].empDesignation);
printf("Employee Salary: %.2f\n",emp[i].empSalary);
printf("\n------------------------------------------------------------\n");
}
}
}
int main()
{
initializeEmployee();
displayEmployeeGettingMoreThanAverageSalary();
return 0;
}
void initializeEmployee(){
//intializing the structure with some values
strcpy(emp[0].empName,"Employee 1");
emp[0].empNumber = 1;
strcpy(emp[0].empDepartment,"IT");
strcpy(emp[0].empDesignation,"Developer");
emp[0].empSalary = 412.00;
strcpy(emp[1].empName,"Employee 2");
emp[1].empNumber = 2;
strcpy(emp[1].empDepartment,"HR");
strcpy(emp[1].empDesignation,"Manager");
emp[1].empSalary = 367.00;
strcpy(emp[2].empName,"Employee 3");
emp[2].empNumber = 3;
strcpy(emp[2].empDepartment,"Finance");
strcpy(emp[2].empDesignation,"Accountant");
emp[2].empSalary = 235.00;
strcpy(emp[3].empName,"Employee 4");
emp[3].empNumber = 4;
strcpy(emp[3].empDepartment,"HR");
strcpy(emp[3].empDesignation,"Manager");
emp[3].empSalary = 567.00;
strcpy(emp[4].empName,"Employee 5");
emp[4].empNumber = 5;
strcpy(emp[4].empDepartment,"Finance");
strcpy(emp[4].empDesignation,"Accountant");
emp[4].empSalary = 678.00;
}
void totalSalaryDrawnInEachDepartment(){
int i;
float ITtotalSalary = 0,HRtotalSalary = 0,FinanceTotalSalary = 0;
//iterating through the structure
for(i=0;i<5;i++){
//calculating the total salary drawn in each department
if(strcmp(emp[i].empDepartment,"IT")==0){
ITtotalSalary += emp[i].empSalary;
}
else if(strcmp(emp[i].empDepartment,"HR")==0){
HRtotalSalary += emp[i].empSalary;
}
else if(strcmp(emp[i].empDepartment,"Finance")==0){
FinanceTotalSalary += emp[i].empSalary;
}
}
//printing the total salary drawn in each department
printf("Total salary drawn in IT department is %.2f\n",ITtotalSalary);
printf("Total salary drawn in HR department is %.2f\n",HRtotalSalary);
printf("Total salary drawn in Finance department is %.2f\n",FinanceTotalSalary);
}
int main()
{
initializeEmployee();
totalSalaryDrawnInEachDepartment();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------
Section 5 ( Arrays and Files)
Q13 Write a C program to define a structure called student with data members, name, register
number, major, marks in core subject1, core subject2, allied, elective, total, average and grade.
The data for name, register number, major, core subject1, core subject2, allied and elective are to
be obtained from user.
a) Calculate total, average and grade for the students and update the data in the file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<math.h>
struct Student
{
char name[20];
int regNo;
char major[20];
float core1Marks;
float core2Marks;
float alliedMarks;
float electiveMarks;
float totalMarks;
float averageMarks;
char grade;
} stu[5];
float generateRandomFloatNumber()
{
float min = 10.0,max=100.0;
float scale = rand() / (float) RAND_MAX;
return min + scale * (max - min);
}
void initializeStudents()
{
int i;
// intializing the structure with some values
for (i = 0; i < 5; i++)
{
snprintf(stu[i].name, 20, "Student %d", i + 1);
stu[i].regNo = 100 + (i + 1);
strcpy(stu[i].major, "Computer Science");
stu[i].core1Marks = generateRandomFloatNumber();
stu[i].core2Marks = generateRandomFloatNumber();
stu[i].alliedMarks = generateRandomFloatNumber();
stu[i].electiveMarks = generateRandomFloatNumber();
}
}
void calculateTotalMarks()
{
int i;
float totalMarks = 0;
for (i = 0; i < 5; i++)
{
totalMarks = 0;
totalMarks = stu[i].core1Marks + stu[i].core2Marks + stu[i].alliedMarks +
stu[i].electiveMarks;
stu[i].totalMarks = totalMarks;
}
}
void calcuateAvergaeMarks()
{
int i;
for(i=0;i<5;i++)
{
stu[i].averageMarks = stu[i].totalMarks/4;
}
}
void calculateGrades(){
int i;
for(i=0;i<5;i++)
{
if(stu[i].averageMarks>=90)
stu[i].grade = 'A';
else if(stu[i].averageMarks>=80)
stu[i].grade = 'B';
else if(stu[i].averageMarks>=70)
stu[i].grade = 'C';
else if(stu[i].averageMarks>=60)
stu[i].grade = 'D';
else
stu[i].grade = 'F';
}
}
void storeStudentDataInFile(){
//creating a file
FILE *fp;
fp = fopen("student.txt", "w");
if (fp == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int i;
for (i = 0; i < 5; i++)
{
fprintf(fp,"Student Name: %s\n", stu[i].name);
fprintf(fp,"Register Number: %d\n", stu[i].regNo);
fprintf(fp,"Marks in Major: %s\n", stu[i].major);
fprintf(fp,"Marks in Core1: %f\n", stu[i].core1Marks);
fprintf(fp,"Marks in Core2: %f\n", stu[i].core2Marks);
fprintf(fp,"Allied Marks: %f\n", stu[i].alliedMarks);
fprintf(fp,"Elective Marks: %f\n", stu[i].electiveMarks);
fprintf(fp,"Total Marks: %f\n", stu[i].totalMarks);
fprintf(fp,"Average Marks: %f\n", stu[i].averageMarks);
fprintf(fp,"Grade: %c\n", stu[i].grade);
fprintf(fp,"\n-----------------------------------------------------\n");
}
fclose(fp);
printf("Data stored in file successfully\n");
}
int main()
{
initializeStudents();
calculateTotalMarks();
calcuateAvergaeMarks();
calculateGrades();
storeStudentDataInFile();
return 0;
}
B) Search for a student whose register number is obtained from the user as console input.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<math.h>
struct Student
{
char name[20];
int regNo;
char major[20];
float core1Marks;
float core2Marks;
float alliedMarks;
float electiveMarks;
float totalMarks;
float averageMarks;
char grade;
} stu[5];
float generateRandomFloatNumber()
{
float min = 10.0,max=100.0;
float scale = rand() / (float) RAND_MAX;
return min + scale * (max - min);
}
void initializeStudents()
{
int i;
// intializing the structure with some values
for (i = 0; i < 5; i++)
{
snprintf(stu[i].name, 20, "Student %d", i + 1);
stu[i].regNo = 100 + (i + 1);
strcpy(stu[i].major, "Computer Science");
stu[i].core1Marks = generateRandomFloatNumber();
stu[i].core2Marks = generateRandomFloatNumber();
stu[i].alliedMarks = generateRandomFloatNumber();
stu[i].electiveMarks = generateRandomFloatNumber();
}
}
void calculateTotalMarks()
{
int i;
float totalMarks = 0;
for (i = 0; i < 5; i++)
{
totalMarks = 0;
totalMarks = stu[i].core1Marks + stu[i].core2Marks + stu[i].alliedMarks +
stu[i].electiveMarks;
stu[i].totalMarks = totalMarks;
}
}
void calcuateAvergaeMarks()
{
int i;
for(i=0;i<5;i++)
{
stu[i].averageMarks = stu[i].totalMarks/4;
}
}
void calculateGrades(){
int i;
for(i=0;i<5;i++)
{
if(stu[i].averageMarks>=90)
stu[i].grade = 'A';
else if(stu[i].averageMarks>=80)
stu[i].grade = 'B';
else if(stu[i].averageMarks>=70)
stu[i].grade = 'C';
else if(stu[i].averageMarks>=60)
stu[i].grade = 'D';
else
stu[i].grade = 'F';
}
}
void searchStudent(){
int regNo,i,isFound=0;
printf("Enter the register number of the student you want to search: ");
scanf("%d",®No);
for(i=0;i<5;i++)
{
if(stu[i].regNo==regNo)
{
isFound = 1;
printf("Student Name: %s\n", stu[i].name);
printf("Register Number: %d\n", stu[i].regNo);
printf("Marks in Major: %s\n", stu[i].major);
printf("Marks in Core1: %f\n", stu[i].core1Marks);
printf("Marks in Core2: %f\n", stu[i].core2Marks);
printf("Allied Marks: %f\n", stu[i].alliedMarks);
printf("Elective Marks: %f\n", stu[i].electiveMarks);
printf("Total Marks: %f\n", stu[i].totalMarks);
printf("Average Marks: %f\n", stu[i].averageMarks);
printf("Grade: %c\n", stu[i].grade);
printf("\n-----------------------------------------------------\n");
}
}
if(isFound==0)
printf("Student not found\n");
}
int main()
{
initializeStudents();
calculateTotalMarks();
calcuateAvergaeMarks();
calculateGrades();
searchStudent();
return 0;
}
C) Find the first three highest averages and print those student’s details
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<math.h>
struct Student
{
char name[20];
int regNo;
char major[20];
float core1Marks;
float core2Marks;
float alliedMarks;
float electiveMarks;
float totalMarks;
float averageMarks;
char grade;
} stu[5];
float generateRandomFloatNumber()
{
float min = 10.0,max=100.0;
float scale = rand() / (float) RAND_MAX;
return min + scale * (max - min);
}
void initializeStudents()
{
int i;
// intializing the structure with some values
for (i = 0; i < 5; i++)
{
snprintf(stu[i].name, 20, "Student %d", i + 1);
stu[i].regNo = 100 + (i + 1);
strcpy(stu[i].major, "Computer Science");
stu[i].core1Marks = generateRandomFloatNumber();
stu[i].core2Marks = generateRandomFloatNumber();
stu[i].alliedMarks = generateRandomFloatNumber();
stu[i].electiveMarks = generateRandomFloatNumber();
}
}
void calculateTotalMarks()
{
int i;
float totalMarks = 0;
for (i = 0; i < 5; i++)
{
totalMarks = 0;
totalMarks = stu[i].core1Marks + stu[i].core2Marks + stu[i].alliedMarks +
stu[i].electiveMarks;
stu[i].totalMarks = totalMarks;
}
}
void calcuateAvergaeMarks()
{
int i;
for(i=0;i<5;i++)
{
stu[i].averageMarks = stu[i].totalMarks/4;
}
}
void calculateGrades(){
int i;
for(i=0;i<5;i++)
{
if(stu[i].averageMarks>=90)
stu[i].grade = 'A';
else if(stu[i].averageMarks>=80)
stu[i].grade = 'B';
else if(stu[i].averageMarks>=70)
stu[i].grade = 'C';
else if(stu[i].averageMarks>=60)
stu[i].grade = 'D';
else
stu[i].grade = 'F';
}
}
void printThreeHighestAverages(){
int main()
{
initializeStudents();
calculateTotalMarks();
calcuateAvergaeMarks();
calculateGrades();
printThreeHighestAverages();
return 0;
}
Q14. Assume that there is an array consisting of ‘n’ elements. Write a program to store all the
prime numbers of the list in a file called as “PRIME” and non-prime numbers in another file called
as “NPRIME”. You have to define a function to check whether an element is a prime or not
#include <stdio.h>
FILE *NprimeNoFile;
NprimeNoFile = fopen("NPRIME.txt", "w");
// if number is prime, then isPrime=1 else isPrime=0
int isPrime = 1;
for (int i = 0; i < size; i++)
{
isPrime = 1;
if(arr[i]==0 || arr[i]==1 ||arr[i]==2){
isPrime=0;
fprintf(NprimeNoFile,"%d\n", arr[i]);
continue;
}
for (int j = 2; j <= arr[i] / 2; j++)
{
if (arr[i] % j == 0 )
isPrime = 0;
}
if (isPrime == 1)
fprintf(primeNoFile,"%d\n", arr[i]);
else
fprintf(NprimeNoFile,"%d\n", arr[i]);
}
fclose(primeNoFile);
fclose(NprimeNoFile);
printf("\nPrime numbers are stored in PRIME.txt file\n");
printf("\nNon Prime numbers are stored in NPRIME.txt file\n");
int main()
{
int arr[100];
for (int i = 1; i <= 100; i++)
{
arr[i] = i;
}
int size = sizeof(arr) / sizeof(arr[0]);
storePrimeNumbers(arr, size);
}