0% found this document useful (0 votes)
15 views14 pages

C Programs for Data Structures and Algorithms

The document contains 10 programming problems that involve using structures in C. The problems cover creating structures to count character frequencies, storing and searching friend contact information, calculating sentence statistics, ranking students by exam scores, summing numbers using functions, finding student birth years, copying one structure to another, sorting arrays with odd and even elements, storing employee information including a nested date structure, and counting word frequencies in a string.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

C Programs for Data Structures and Algorithms

The document contains 10 programming problems that involve using structures in C. The problems cover creating structures to count character frequencies, storing and searching friend contact information, calculating sentence statistics, ranking students by exam scores, summing numbers using functions, finding student birth years, copying one structure to another, sorting arrays with odd and even elements, storing employee information including a nested date structure, and counting word frequencies in a string.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

ASSIGNMENT 10

[Link] a program that determines and displays the number of times, such that
characters appears in a given sentence (histogram) entered by the user. Use a structure to
solve this problem.
#include<stdio.h>

struct histogram

char letters;

int count;

}h[26];

int main()

char a[100];

printf("Enter the sentence \n");

scanf("%[^\n]s",a);

for(int i=0;i<26;i++)

h[i].letters='a'+i;

h[i].count=0;

for(int i=0;a[i];i++)

char pre = a[i];

if('a'<=pre && pre<='z')

h[pre-'a'].count++;

else if('A'<=pre && pre<='Z')

h[pre-'A'].count++;

}
for(int i=0;i<26;i++)

if(h[i].count>0)

printf("%c : %d\n",h[i].letters,h[i].count);

2. Write a program which takes as input your friends names, their phone number and stores them
in a structure Perform the following operations on the structure.

i) Search for a given friend and print his details if present in the structure and otherwise, print not
found.

#include<stdio.h>

#include<string.h>

struct friends

char name[50];

float number;

}f[50];

int main()

int n,found=1;

char search[50];

printf("Enter the number of friends \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the name %d : ",i+1);

scanf(" %[^\n]",f[i].name);

printf("Enter the number %d : ",i+1);

scanf("%lf",&f[i].number);

printf("Enter the friend to be searched : ");

scanf(" %[^\n]",search);
for(int i=0;i<n;i++)

if(stricmp(f[i].name,search)==0)

printf("Name : %s Phone number : %lf",f[i].name,f[i].number);

found = 1;

break;

if(i==n-1)

printf("Not found");

ii)Sort the structure in alphabetical order of names and as well using the phone numbers.

#include<stdio.h>

#include<string.h>

struct friends

char name[50];

long int number;

}f[50];

int main()

int n,k=0;

long int temp;

char t[50];

printf("Enter the number of friends \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the name %d : ",i+1);


scanf("%s",f[i].name);

printf("Enter the number %d : ",i+1);

scanf("%ld",&f[i].number);

for(int i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if(strcmp(f[i].name , f[j].name)>0)

strcpy(t,f[i].name);

strcpy(f[i].name,f[j].name);

strcpy(f[j].name,t);

temp=f[i].number;

f[i].number=f[j].number;

f[j].number=temp;

printf("%s : %ld \n",f[i].name,f[i].number);

3. Write a C program that accepts a sentence from the user and displays the sentence statistics by
creating a structure.

#include<stdio.h>

#include<string.h>

struct statistics

int words;

int vowels;
int digits;

};

int main()

char a[100];

printf("Enter the string \n");

scanf("%[^\n]s",a);

struct statistics s = {1,0,0};

for(int i=0;i<strlen(a);i++)

if(a[i]>='0' && a[i]<='9')

[Link]++;

if(a[i]==' ')

if(!(a[i+1]>='0' && a[i+1]<='9') && !(a[i-1]>='0'&&a[i-1]<='9'))

[Link]++;

if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||
a[i]=='U')

[Link]++;

printf("The number of words : %d\nThe number of vowels : %d\nThe number of digits : %d\
n",[Link],[Link],[Link]);

4. Write a program to compute the rank of n number of students in a class based on

total marks obtained. Input details include Roll Number, Name, Mathematics Mark,

Physics Mark and Chemistry mark for two assessments. Design a suitable structure

and output should include input details along with total mark,

computed rank and serial number.

#include<stdio.h>
#include<string.h>

struct student

int roll;

char name[100];

int phy1,phy2;

int chem1,chem2;

int math1,math2;

int tot;

}s[50];

int main()

char z[100];

int n,t;

printf("Enter the number of students \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the name of %d student : ",i+1);

scanf("%s",s[i].name);

printf("Enter the rollnum of student : ");

scanf("%d",&s[i].roll);

printf("Enter the physics mark 1 : ");

scanf("%d",&s[i].phy1);

printf("Enter the physics mark 2 : ");

scanf("%d",&s[i].phy2);

printf("Enter the chemistry mark 1 : ");

scanf("%d",&s[i].chem1);

printf("Enter the chemistry mark 2 : ");

scanf("%d",&s[i].chem2);

printf("Enter the maths mark 1 : ");


scanf("%d",&s[i].math1);

printf("Enter the maths mark 2 : ");

scanf("%d",&s[i].math2);

s[i].tot=s[i].math1+s[i].math2+s[i].phy1+s[i].phy2+s[i].chem1+s[i].chem2;

for(int i=0;i<n;i++)

for(int j=i+1;j<n;j++)

if(s[i].tot<s[j].tot)

t=s[i].tot;

s[i].tot=s[j].tot;

s[j].tot=t;

t=s[i].roll;

s[i].roll=s[j].roll;

s[j].roll=t;

t=s[i].math1;

s[i].math1=s[j].math1;

s[j].math1=t;

t=s[i].math2;

s[i].math2=s[j].math2;

s[j].math2=t;

t=s[i].phy1;

s[i].phy1=s[j].phy1;

s[j].phy1=t;
t=s[i].phy2;

s[i].phy2=s[j].phy2;

s[j].phy2=t;

t=s[i].chem1;

s[i].chem1=s[j].chem1;

s[j].chem1=t;

t=s[i].chem2;

s[i].chem2=s[j].chem2;

s[j].chem2=t;

strcpy(z,s[i].name);

strcpy(s[i].name,s[j].name);

strcpy(s[j].name,z);

printf("%d : %s\t%d rank\ttotal : %d phy1 : %d phy2 : %d chem1 : %d chem2 : %d math1 : %d


math2 : %d\n\n "

,s[i].roll,s[i].name,i+1,s[i].tot,s[i].phy1,s[i].phy2,s[i].chem1,s[i].chem2,s[i].math1,s[i].math2);

5. Write a program using function to calculate the sum of all numbers between 1 and n.

#include<stdio.h>

int sum(int n)

int sum=0;

for(int i=1;i<=n;i++)

sum=sum+i;

return sum;

}
int main()

int n;

printf("Enter the number of terms \n");

scanf("%d",&n);

printf("The sum is %d",sum(n));

6. Declare an array of structures, where each structure has student register number, day , month
and year of birth. Get input from the user for a class of 15 students. Write a completer c program
to find the number of students who were born in a given years.

#include<stdio.h>

struct student

int rollnum;

char day;

char month;

int year;

}s[50];

int main()

int n,search,count=0;

printf("Enter the number of students \n");

scanf("%d",&n);

for(int i=0;i<n;i++)

printf("Enter the register number %d : ",i+1);

scanf("%d",&s[i].rollnum);

printf("Enter the day of birth : ");

scanf("%s",&s[i].day);

printf("Enter the month of birth : ");

scanf("%s",&s[i].month);
printf("Enter the year of birth : ");

scanf("%d",&s[i].year);

printf("Enter the required year : ");

scanf("%d",&search);

for(int i=0;i<n;i++)

if(s[i].year==search)

count++;

if(count>0)

printf("%d students were born in %d year",count,search);

if(count == 0)

printf("%d year is not found",search);

7. Write a C program to demonstrate copying one structure to another of same type.

#include<stdio.h>

#include<string.h>

struct student

char name[50];

int rollnum;

char department[50];

}std1;

struct copy

char name[50];

int rollnum;

char department[50];

}std2;

int main()
{

struct student std1 = {"luffy ",ECE,"Batch I"};

strcpy([Link],[Link]);

[Link]=[Link];

strcpy([Link],[Link]);

printf("The copied structure is Name : %s Rollnum : %d Department :


%s",[Link],[Link],[Link]);

8. Write a program to arrange elements in the list in such a way that all odd elements

appear first and all even elements appear at the end of the array. No additional

array must be used. Odd and even values should maintain their relative order within

each group as in original array.

#include<stdio.h>

int main()

int a[20],n,t,oddindex=0;

printf("Enter the number of elements to be in the array \n");

scanf("%d",&n);

printf("Enter the elements \n");

for(int i=0;i<n;i++)

scanf("%d",&a[i]);

for(int i=0;i<n;i++)

if(a[i]%2 == 1)

t=a[i];

for(int j=i;j>oddindex;j--)

a[j]=a[j-1];

a[oddindex]=t;

oddindex++;

}
}

for(int i=0;i<n;i++)

printf("%d ",a[i]);

9. Write a program that stores (in a structure) and then displays information about an

employee such as name, DOB, gender, designation, department, and salary. The

date of birth (DOB) attribute of an employee should be stored as a nested structure

with day, month and year attributes

#include<stdio.h>

struct date

char day[50];

int month;

int year;

}DOB;

struct employee

char name[50];

struct date DOB;

char gender[50];

char designation[50];

char department[50];

int salary;

}e;

int main()

printf("Enter the name of the employee : ");

scanf(" %[^\n]",&[Link]);

printf("Enter the DOB of the employee : ");

scanf("%s",&[Link]);
scanf("%d",&[Link]);

scanf("%d",&[Link]);

printf("Enter the gender of the employee : ");

scanf(" %[^\n]",&[Link]);

printf("Enter the designation of the employee : ");

scanf(" %[^\n]",&[Link]);

printf("Enter the department of the employee : ");

scanf(" %[^\n]",&[Link]);

printf("Enter the salary of the employee : ");

scanf("%d",&[Link]);

printf("Name : %s DOB : %s %d %d Gender : %s Designation : %s Department : %s Salary :


%d",[Link],[Link],[Link],[Link],[Link],[Link],[Link],[Link]);

10. Write a program to print the frequency of each word of input.

#include<stdio.h>

#include<string.h>

int main()

char a[100],b[50][50];

int frequency[50]={0},words=0;

printf("Enter the string \n");

scanf("%[^\n]s",&a);

char *token = strtok(a," ");

while(token != NULL)

strcpy(b[words],token);

frequency[words]++;

words++;

token=strtok(NULL," ");

for(int i=0;i<words;i++)
{

for(int j=i+1;j<words;j++)

if(stricmp(b[i],b[j])==0)

frequency[i]++;

frequency[j]=0;

if(frequency[i]!=0)

printf("%s : %d\n",b[i],frequency[i]);

Common questions

Powered by AI

The algorithm involves iterating over the array elements and using a variable oddindex to track the position of the last odd number. When an odd number is encountered, it shifts all elements between this and oddindex forward by one position and places the odd number at the oddindex. The oddindex is then incremented. This ensures all odd numbers appear before even numbers while preserving relative order .

The program defines two structures of the same type and directly copies field values from one structure to another using simple assignment for non-string fields and strcpy for string fields. This demonstrates shallow copying where structure fields are duplicated without requiring extra memory for new structures .

The program uses a 'student' structure to collect marks from two assessments in physics, chemistry, and mathematics. Each student's total marks are calculated by summing these scores. The structure is then sorted in descending order based on total marks using nested loops for comparisons and swaps. The output displays student details along with their computed total marks and rank .

The program defines a 'statistics' structure with counters for words, vowels, and digits. The user inputs a sentence, and the program iterates over each character. It increments the digit counter if a character is a digit, counts vowels by checking against a set list of vowel characters, and appraises word count based on spaces that aren't preceded or succeeded by a digit. After processing, it prints the statistics .

The program uses an array of 'friends' structures, each storing a name and phone number. The user inputs data for a specified number of friends. For searching, the program compares the search term with each stored name using a case-insensitive comparison function (stricmp). If a match is found, it displays the friend's details; otherwise, it prints 'not found' .

The program uses a 'date' structure nested within an 'employee' structure to store the employee's date of birth along with other details like name, gender, designation, department, and salary. The user inputs all attributes, and the program stores them in the appropriate fields. Finally, it prints all stored information, demonstrating the process of accessing nested structure members .

The program defines a function that iterates through numbers from 1 to n, accumulating their sum in a variable. It returns the resulting sum, which is then printed in the main function. This approach directly implements the arithmetic series sum calculation by iterative addition .

The program initializes an array of structures, each containing a letter and its count, for all letters of the alphabet. Upon receiving an input sentence, it iterates through each character. If the character is a lowercase or uppercase letter, it increments the corresponding count in the array for that letter by calculating its position using ASCII values. Finally, it prints the letter and its frequency if the count is greater than zero .

The program uses a nested loop to compare each friend's name with others. If the current friend's name is lexicographically greater than the next friend's, it swaps their names and phone numbers using a temporary variable for each. This effectively sorts the names in ascending order .

The program tokenizes the input string into words using the strtok function, and each word is stored in an array. It iterates through the words, incrementing a frequency count for words matching previous entries using a case-insensitive comparison. Finally, it prints each unique word with its frequency, skipping already processed duplicates .

You might also like