Reputation: 96
My project needs me to add names then grades, alternating between each person per assignment. So I enter the number of students, then the number of assignments, the students names, then their grades.
My problem is my print function is printing the grades straight down in a column instead of printing in rows. I think I'm missing something, my print function is called print_report.
#include <stdio.h>
#include <string.h>
int numStus;
int numGrades;
// Get students names
void get_students(char stuNames[numStus][10])
{
int i;
int j;
for (i=0; i<numStus; i++)
{
printf("Enter the students name.");
scanf("%s", &stuNames[i]);
}
}
// enter each students grades
void get_grades(int grades[numGrades][numStus], char stuNames[numStus] [10])
{
int x;
int z;
for (x=0; x < numGrades; x++)
{
for (z = 0; z < numStus; z++)
{
printf("Enter score for %s assignment %d: ", stuNames[z],x);
scanf("%d", &grades[x][z]);
}
}
}
void calc_grades( );
void print_report(int grades[numGrades][numStus], char stuNames[numStus] [10])
{
int i;
int j;
int k;
for(i = 0; i < numStus; i++)
{
printf("%s ",stuNames[i]);
}
printf("\n");
for(j = 0; j < numGrades; j++)
{
for(k = 0; k < numStus; k++)
{
printf("%6i",grades[j][k] );
printf("\n");
}
}
}
int main()
{
printf("How many students?");
scanf("%d", &numStus);
printf("How many assignments?");
scanf("%d", &numGrades);
// Ask how many students
// Ask how many assignments
// Initializations...
char stuNames[numStus][10];
int grades[numGrades][numStus];
char finalGrades[numStus];
//Get Student Names
get_students(stuNames);
get_grades(grades, stuNames);
print_report(grades, stuNames);
int x;
int z;
// Get Grades
// Calculate Final Grades
// Print Report
return 0;
}
Upvotes: 1
Views: 2114
Reputation: 88
What you have to do is look at your print_report function. Move the printf("\n") statement outside the nested for loop to the outer for loop. Like this:
for(j = 0; j < numGrades; j++)
{
for(k = 0; k < numStus; k++)
{
printf("%6i",grades[j][k] );
}
printf("\n");
}
I am not sure if this is the problem that you were having, but my intuition tells me that it is.
Upvotes: 2