0% found this document useful (0 votes)
13 views

Annotated-C Assignment 202017b3723

The document contains code snippets for various C programming questions related to strings, sorting/searching algorithms, loops/conditions, pointers and structures. Specifically, it includes code to count articles in a paragraph, count lines/words/characters in multiple paragraphs, manually implement insertion and selection sort, find strong and cyclic numbers, sort employee records by department and salary, and calculate average employee salary. The code snippets demonstrate the use of various C programming concepts like strings, arrays, structures, sorting algorithms, loops and conditional statements.

Uploaded by

mohit kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Annotated-C Assignment 202017b3723

The document contains code snippets for various C programming questions related to strings, sorting/searching algorithms, loops/conditions, pointers and structures. Specifically, it includes code to count articles in a paragraph, count lines/words/characters in multiple paragraphs, manually implement insertion and selection sort, find strong and cyclic numbers, sort employee records by department and salary, and calculate average employee salary. The code snippets demonstrate the use of various C programming concepts like strings, arrays, structures, sorting algorithms, loops and conditional statements.

Uploaded by

mohit kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Name – Mohit Kumar

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++;

token = strtok(NULL, " ");


}
printf("\nTotal Number of articles in the given paragraph is: %d", 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);
}
-----------------------------------------------------------------------------------------------------------------------------------
---

Section 2( Sorting and Searching)


Q4. Execute insertion sort algorithm manually to sort the following numbers 55, 23, 45, 12, 67, 20
, 34, 10 , 54, 50, 19
#include<stdio.h>
void insertionSort(int arr[], int size)
{
int i,j,temp;
for (i = 1; i < size; i++)
{
temp = arr[i];
j = i - 1;

while (arr[j] > temp && j >= 0)


{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = temp;
}
printf("Sorted Array is : ");
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}
int main()
{
int arr[]={55, 23, 45, 12, 67, 20 , 34, 10 , 54, 50, 19};
int size = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, size);
}
-----------------------------------------------------------------------------------------------------------------------------------
---
Q5.Execute Selection sort algorithm manually to sort the following numbers 55, 23, 45, 12, 67, 20
, 34, 10 , 54, 50, 19
#include<stdio.h>
void selectionSort(int arr[],int size)
{
int i,j,min,temp;
for(i=0;i<size-1;i++)
{
min=i;
for(j=i+1;j<size;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
}
temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}

printf("Sorted array is: ");


for(i=0;i<size;i++)
printf("%d ",arr[i]);

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);
}
-----------------------------------------------------------------------------------------------------------------------------------
---

Section 3 ( Loops and Conditions)


Q7. Write a program to display all the strong numbers between 100 to 999
#include <stdio.h>
void printStrongNumbers(int from, int to)
{
int num, i, j, fact, rem, sum = 0, temp;
printf("\nStrong numbers between %d and %d are:\n", from, to);
for (i = from; i <= to; i++)
{
num = i;
temp = num;
while (num)
{
j = 1, fact = 1;
rem = num % 10;
while (j <= rem)
{
fact = fact * j;
j++;
}
sum = sum + fact;
num = num / 10;
}
if (sum == temp)
printf("%d\n", temp);
sum = 0;
}
}
int main(){
printStrongNumbers(100,999);
}
--------------------------------------------------------------------------------------------------------------------------------
Q8. Write a program to display all the cyclic numbers between 1 to 99.
#include <stdio.h>
#include <math.h>
void displayCyclicNumbers(int from, int to){
int i,rem,temp,squareRoot;
printf("\nCyclic Numbers between %d and %d are:\n",from,to);
for(i=from;i<=to;i++){
rem = i%10;
squareRoot = i*i;
temp = squareRoot%10;
if(rem == temp)
printf("%d\n",i);
}
}
int main()
{
displayCyclicNumbers(1,99);
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------
---
Section 4 ( Pointers and Structures)
Q10. Assume that there are ‘n’ employee records. Each employee record consists of employee
name, employee number, department, designation and salary. Write a program to sort the records
according to department and 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 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;
}
}
}

//within department sort based on salary


for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(strcmp(emp[i].empDepartment,emp[j].empDepartment)==0){
if(emp[i].empSalary<emp[j].empSalary){
struct employee temp;
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
}
}
void printEmployee(){
//printing the employee details
for(int i=0;i<5;i++){
printf("\nEmployee Name: %s",emp[i].empName);
printf("\nEmployee Number: %d",emp[i].empNumber);
printf("\nEmployee Department: %s",emp[i].empDepartment);
printf("\nEmployee Designation: %s",emp[i].empDesignation);
printf("\nEmployee Salary: %.2f",emp[i].empSalary);
printf("\n-------------------------------------------\n");
}
}

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;
}

c) Find the total salary drawn by employees in each department


#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 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",&regNo);
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(){

//sort the students based on average marks


int i,j;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(stu[i].averageMarks<stu[j].averageMarks)
{
struct Student temp;
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
printf("\n\nThree highest averages are:\n");
for(i=0;i<3;i++)
{
printf("\nStudent 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");
}
}

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>

void storePrimeNumbers(int arr[], int size)


{
FILE *primeNoFile;
primeNoFile = fopen("PRIME.txt", "w");

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);
}

You might also like