please fix this c program. it\'s supposed to generate two initials as Ids for te
ID: 3687238 • Letter: P
Question
please fix this c program. it's supposed to generate two initials as Ids for ten students but instead is generating numbers for the students. please fix it to generate two initials for each of the ten students both uppercase letters between A and Z
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<assert.h>
struct student{
char initials[2];
int score;
};
struct student* allocate(){
/*Allocate memory for ten students*/
struct student* stu = malloc(10 * sizeof(struct student));
/*return the pointer*/
return stu;
}
void generate(struct student* students){
/*Generate random initials and scores for ten students.
The two initial letters must be capital and must be between A and Z.
The scores must be between 0 and 100*/
int i, j;
int unique = 0;// To check for unique initials
char c1, c2;
c1 = rand() % 26 + 'A';
c2 = rand() % 26 + 'A';
for (i = 1; i < 10; i++)
{
while (unique == 0)
{
students[i].initials[0] = c1; // Randomize the Student initialss as well. Create quickie algorithm to check uniqueness.
students[i].initials[1] = c2;
for (j = 0; j < i; j++)
{
if (students[i].initials == students[j].initials)
{
unique = 0;
break;
}
else
{
unique = 1;
}
}
}
unique = 0;
students[i].score = ((rand() % 100) + 1);
}
}
void output(struct student* students){
/*Output information about the ten students in the format:
1. Initials Score
2. Initials Score
...
10. Initials Score*/
int l;
for (l = 0;l < 10; l++)
{
printf("ID: %d ", students[l].initials);
printf("Score: %d ", students[l].score);
}
}
void summary(struct student* students){
/*Compute and print the minimum, maximum and average scores of the ten students*/
int i, sum = 0, min, max;
float avg;
for (i = 0; i < 10; i++)
{
sum += students[i].score;
}
avg = (sum / 10);
min = students[0].score;
max = students[0].score;
for (i = 0; i<10; i++)
{
if (students[i].score < min)
{
min = students[i].score;
}
}
for (i = 0; i<10; i++)
{
if (students[i].score > max)
{
max = students[i].score;
}
}
printf("The minimum score is: %d ", min);
printf("The maximum score is: %d ", max);
printf("The average score is: %g ", avg);
}
void deallocate(struct student* stu){
/*Deallocate memory from stud*/
assert(stu != NULL); // Break off program if null, so we don't get a segmentation fault in freeing the memory.
free(stu);
}
int main(){
srand(time(NULL));
struct student* st = NULL;
/*call allocate*/
st = allocate();
/*call generate*/
generate(st);
/*call output*/
output(st);
/*call summary*/
summary(st);
/*call deallocate*/
deallocate(st);
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#include<assert.h>
struct student{
char initials[2];
int score;
};
struct student* allocate(){
/*Allocate memory for ten students*/
struct student* stu = malloc(10 * sizeof(struct student));
/*return the pointer*/
return stu;
}
void generate(struct student* students){
/*Generate random initials and scores for ten students.
* The two initial letters must be capital and must be between A and Z.
* The scores must be between 0 and 100*/
int i, j;
int unique;// To check for unique initials
char c1, c2;
for (i = 0; i < 10; i++){
unique = 1;
c1 = rand() % 26 + 'A';
c2 = rand() % 26 + 'A';
//while (unique == 0){
students[i].initials[0] = c1; // Randomize the Student initialss as well. Create quickie algorithm to check uniqueness.
students[i].initials[1] = c2;
for (j = 0; j < i; j++){
if (students[i].initials == students[j].initials){
unique = 0;
break;
}
}
if(unique == 0){
i--;
continue;
}
students[i].score = ((rand() % 100) + 1);
}
}
void output(struct student* students){
/*Output information about the ten students in the format:
* 1. Initials Score
* 2. Initials Score
* ...
* 10. Initials Score*/
int l;
for (l = 0;l < 10; l++){
printf("ID: %s ", students[l].initials);
printf("Score: %d ", students[l].score);
}
}
void summary(struct student* students){
/*Compute and print the minimum, maximum and average scores of the ten students*/
int i, sum = 0, min, max;
float avg;
for (i = 0; i < 10; i++){
sum += students[i].score;
}
avg = (sum / 10);
min = students[0].score;
max = students[0].score;
for (i = 0; i<10; i++){
if (students[i].score < min){
min = students[i].score;
}
}
for (i = 0; i<10; i++){
if (students[i].score > max){
max = students[i].score;
}
}
printf("The minimum score is: %d ", min);
printf("The maximum score is: %d ", max);
printf("The average score is: %g ", avg);
}
void deallocate(struct student* stu){
/*Deallocate memory from stud*/
assert(stu != NULL); // Break off program if null, so we don't get a segmentation fault in freeing the memory.
free(stu);
}
int main(){
srand(time(NULL));
struct student* st = NULL;
/*call allocate*/
st = allocate();
/*call generate*/
generate(st);
/*call output*/
output(st);
/*call summary*/
summary(st);
/*call deallocate*/
deallocate(st);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.