Question
1) (10 pts) A class has n students, and the ith student has taken si tests....
Answers
I have also implemened storing lengths of each student's scores array, this is useful in printing the input data and verify whether data has been properly read and stored. The code highlighted in YELLOW is not required for this question. Since, it has been mentioned only input reading and storing is required and storing lengths is not required.
I have used malloc for dynamically allocating memory.
I have included my code and screenshots in this answer. In case, there is any indentation issue due to editor, then please refer to code screenshots to avoid confusion.
------------------main.c-----------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main()
{
int **studentscores; //similar to two dimensional array for storing scores of all studemts
int i, j, numStudents, numScores;
scanf("%d", &numStudents); //read numnber of studentsint *lengths; /* to store lengths of students marks araray (This is not required for this question)
but i did it to print the entered data and verify if data is insert properly) */studentscores = (int **)malloc(numStudents * sizeof(int *)); //allocate space for numStudents
lengths = (int *)malloc(numStudents * sizeof(int)); //allocate space for numStudents sized arrayfor(i = 0; i < numStudents; i++) //for each student
{
scanf("%d", &numScores); //read number of scores
lengths[i] = numScores; //store number of scores as length for ith student
studentscores[i] = (int *)malloc(numScores * sizeof(int)); //get space for storing marks for each student
for(j = 0; j < numScores; j++)
scanf("%d", &studentscores[i][j]); //read each score
}printf("\nData read successfully....\n");
for(i = 0; i < numStudents; i++) //print each student's data
{
printf("Student %d scores are : \n", i+1);
for(j = 0; j < lengths[i]; j++)
printf("%d ", studentscores[i][j]);
printf("\n\n");
}
return 0;
}------------------Screenshot main.c-----------
------------------Output----------
------------------------------------
I hope this helps you,
Please rate this answer if it helped you,
Thanks for the opportunity
-/c/temp/main.c - Sublime Text (UNREGISTERED) 2:56 PM * main.c input.txt #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int **studentscores; //similar to two dimensional array for storing scores of all students int i, j, numStudents, numScores; scanf("%d", &numStudents); //read numnber of students 5 int *lengths; /* to store lengths of students marks araray (This is not required for this question) but i did it to print the entered data and verify if data is insert properly) */ studentscores = (int **)malloc(numStudents * sizeof(int *)); //allocate space for numStudents lengths = (int *)malloc(numStudents * sizeof(int)); //allocate space for numStudents sized array for(i = 0; i < numStudents; i++) //for each student 18 19 20 21 22 23 24 25 26 scanf("%d", &numScores); //read number of scores lengths[i] = numScores; //store number of scores as length for ith student studentscores[i] = (int *)malloc(numScores * sizeof(int)); //get space for storing marks for each student for(j = 0; j < numScores; j++) scanf("%d", &studentscores[i][j]); //read each score } printf("\nData read successfully....\n"); for(i = 0; i < numStudents; i++) //print each student's data printf("Student $d scores are : \n", i+1); for(j = 0; j < lengths[i]; j++) printf("d ", studentscores[i][j]); printf("\n\n"); } 33 34 35 36 return 0; } Line 27, Column 75 Tab Size: 4/c/temp/input.txt - Sublime Text (UNREGISTERED) 2:52 PM 1 2 3 4 5 6 main.c. X input.txt 4 10 100 80 90 100 90 90 95 80 100 95 3 87 93 90 5 100 90 90 100 90 8 90 90 90 90 80 80 85 85 Line 6 Column 1 Tab Size: 4 Plain Text2:52 PM [email protected]: -/c/temp [email protected]:-/c/temps gcc main.c [email protected]:-/c/temp$ ./a.out < input.txt Data read successfully.... Student 1 scores are : 100 80 90 100 90 90 95 80 100 95 Student 2 scores are : 87 93 90 Student 3 scores are : 100 90 90 100 90 Student 4 scores are : 90 90 90 90 80 80 85 85 [email protected]:-/c/temp$ I