My professor assigned an assignment where we have to find the frequency of each
ID: 665704 • Letter: M
Question
My professor assigned an assignment where we have to find the frequency of each character in a given text file that contains "xyxabcabcdabcdefa" and it is named chars.txt. In the assignment, we have three functions, a structure, and a main to use. The functions are named as followed:
• int charInCFStruct(charfreq **cfarray, int size, char c) - Returns the index of charfreq struct which has the char c stored in its member variable next. If none of the charfreq structs contains char c then it returns -1.
• void printCFStruct(charfreq **cfarray, int size) - Prints the contents of all of the charfreq structs
• void freeCFStruct(charfreq **cfarray, int size) - Frees all of the charfreq structs and then frees the pointer to the structs.
and our structure is given as:
However, the professor has given us the main but it is incomplete. The student must complete the missing sections which is where I am stuck. (NOTE: I CANNOT change anything else in the main. I can only add into the missing section. )
The main is given as:
Explanation / Answer
int main()
{
struct detail s[26];
char string[100], c;
int i = 0, index;
for (i = 0; i < 26; i++)
{
s[i].c = i + 'a';
s[i].freq = 0;
}
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;
if (c == ' ')
{
break;
}
c = tolower(c);
index = c - 'a';
s[index].freq++;
} while (1);
string[i - 1] = '';
printf("The string entered is: %s ", string);
printf("************************* Character Frequency ************************* ");
for (i = 0; i < 26; i++)
{
if (s[i].freq)
{
printf(" %c %d ", s[i].c, s[i].freq);
}
}
struct student list[50];
int i,j,n;
clrscr();
printf("enter how many student ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter rollno,date of birth ");
scanf("%d%d%d%d", &list[i].rl,&list[i].dob.dd,
&list[i].dob.mm,&list[i].dob.yy);
list[i].t=0;
printf("enter three subject marks ");
for(j=0;j<3;j++)
{
scanf("%d",&list[i].m[j]);
list[i].t+=list[i].m[j];
}
}
printf("student list ");
for(i=0;i<n;i++)
{
printf("%d %d-%d-%d ",list[i].rl,
list[i].dob.dd,
list[i].dob.mm,
list[i].dob.yy);
for(j=0;j<3;j++)
printf("%d ",list[i].m[j]);
printf("%d ",list[i].t);
}
struct student_detail stu_data = {1, "Raju", 90.5, 71145,
"Anna University"};
stu_data_ptr = &stu_data;
printf(" Id is: %d ", stu_data_ptr->id);
printf(" Name is: %s ", stu_data_ptr->name);
printf(" Percentage is: %f ",
stu_data_ptr->percentage);
printf(" College Id is: %d ",
stu_data_ptr->clg_data.college_id);
printf(" College Name is: %s ",
stu_data_ptr->clg_data.college_name);
return 0;
}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.