How can I take the average of only certain rows and columns of a 2D array?
up vote
0
down vote
favorite
I have to take the average of only the test grades and put them into the last column but I have no idea how to do it. I have the function for the 2D array that takes in a filename that contains the grades but the first column is the student id so I don't need to take the average of that.
This is the code I have for the 2D array function.
#define x 10
#define y 6
void getData(float arr1[x][y])
FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter the text filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;
for(int i = 0; i < x; i++)
if(graFile != NULL)
fscanf(graFile, "%d%d%d%d%d", &studentId, &test1, &test2, &test3, &test4);
arr2[i][0] = studentId;
arr2[i][1] = test1;
arr2[i][2] = test2;
arr2[i][3] = test3;
arr2[i][4] = test4;
else
printf("nThis file does not exist.");
return;
printf("n %11s%11s%11s%11s%11s%11s", "Student Id","Test 1","Test 2","Test 3","Test 4","Finaln");
printf("*********************************************************************n");
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
printf("%11.0f", arr2[i][j]);
printf("n");
printf("*********************************************************************n");
fclose(graFile);
return;
That gives me this output
Enter the text filename: grades.txt
Student Id Test 1 Test 2 Test 3 Test 4 Final
*********************************************************************
6814 85 86 92 88 0
7234 76 81 84 78 0
6465 87 54 68 72 0
7899 92 90 88 86 0
9901 45 78 79 80 0
8234 77 87 84 98 0
7934 76 91 84 65 0
7284 56 81 87 98 0
7654 76 87 84 88 0
3534 86 81 84 73 0
*********************************************************************
Now I just need to create a new function that averages the test grades from this array and put it into the last column but I'm having trouble doing that. I appreciate any help I can get.
c arrays function average
add a comment |
up vote
0
down vote
favorite
I have to take the average of only the test grades and put them into the last column but I have no idea how to do it. I have the function for the 2D array that takes in a filename that contains the grades but the first column is the student id so I don't need to take the average of that.
This is the code I have for the 2D array function.
#define x 10
#define y 6
void getData(float arr1[x][y])
FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter the text filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;
for(int i = 0; i < x; i++)
if(graFile != NULL)
fscanf(graFile, "%d%d%d%d%d", &studentId, &test1, &test2, &test3, &test4);
arr2[i][0] = studentId;
arr2[i][1] = test1;
arr2[i][2] = test2;
arr2[i][3] = test3;
arr2[i][4] = test4;
else
printf("nThis file does not exist.");
return;
printf("n %11s%11s%11s%11s%11s%11s", "Student Id","Test 1","Test 2","Test 3","Test 4","Finaln");
printf("*********************************************************************n");
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
printf("%11.0f", arr2[i][j]);
printf("n");
printf("*********************************************************************n");
fclose(graFile);
return;
That gives me this output
Enter the text filename: grades.txt
Student Id Test 1 Test 2 Test 3 Test 4 Final
*********************************************************************
6814 85 86 92 88 0
7234 76 81 84 78 0
6465 87 54 68 72 0
7899 92 90 88 86 0
9901 45 78 79 80 0
8234 77 87 84 98 0
7934 76 91 84 65 0
7284 56 81 87 98 0
7654 76 87 84 88 0
3534 86 81 84 73 0
*********************************************************************
Now I just need to create a new function that averages the test grades from this array and put it into the last column but I'm having trouble doing that. I appreciate any help I can get.
c arrays function average
arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
– rainhaven
Nov 11 at 3:04
Besides the obvious answer above, your program has several issues: 1. if(graFile != NULL) needs to be outside the for loop, not inside. 2. return value of fscanf needs to be checked; unless you're absolutely sure the file always contains at least x rows and the format is always consistent with your fscanf.
– rainhaven
Nov 11 at 3:07
Do you know how I could do that in a function of its own? The file only contains that amount of rows because this is a project for school so it will never be modified. So once I put the if condition outside the loop will my code be okay?
– Angie
Nov 11 at 3:11
A function take a float[x][y] as parameter, where the 1st column is student id, 2-5 columns are scores, the function saves the average to the last (6th) column. Is this you need?
– rainhaven
Nov 11 at 3:21
Yes, exactly that.
– Angie
Nov 11 at 3:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have to take the average of only the test grades and put them into the last column but I have no idea how to do it. I have the function for the 2D array that takes in a filename that contains the grades but the first column is the student id so I don't need to take the average of that.
This is the code I have for the 2D array function.
#define x 10
#define y 6
void getData(float arr1[x][y])
FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter the text filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;
for(int i = 0; i < x; i++)
if(graFile != NULL)
fscanf(graFile, "%d%d%d%d%d", &studentId, &test1, &test2, &test3, &test4);
arr2[i][0] = studentId;
arr2[i][1] = test1;
arr2[i][2] = test2;
arr2[i][3] = test3;
arr2[i][4] = test4;
else
printf("nThis file does not exist.");
return;
printf("n %11s%11s%11s%11s%11s%11s", "Student Id","Test 1","Test 2","Test 3","Test 4","Finaln");
printf("*********************************************************************n");
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
printf("%11.0f", arr2[i][j]);
printf("n");
printf("*********************************************************************n");
fclose(graFile);
return;
That gives me this output
Enter the text filename: grades.txt
Student Id Test 1 Test 2 Test 3 Test 4 Final
*********************************************************************
6814 85 86 92 88 0
7234 76 81 84 78 0
6465 87 54 68 72 0
7899 92 90 88 86 0
9901 45 78 79 80 0
8234 77 87 84 98 0
7934 76 91 84 65 0
7284 56 81 87 98 0
7654 76 87 84 88 0
3534 86 81 84 73 0
*********************************************************************
Now I just need to create a new function that averages the test grades from this array and put it into the last column but I'm having trouble doing that. I appreciate any help I can get.
c arrays function average
I have to take the average of only the test grades and put them into the last column but I have no idea how to do it. I have the function for the 2D array that takes in a filename that contains the grades but the first column is the student id so I don't need to take the average of that.
This is the code I have for the 2D array function.
#define x 10
#define y 6
void getData(float arr1[x][y])
FILE* graFile;
float arr2[x][y];
char userIn[50];
printf("Enter the text filename: ");
scanf("%s", userIn);
graFile = fopen(userIn, "r");
int studentId, test1, test2, test3, test4;
for(int i = 0; i < x; i++)
if(graFile != NULL)
fscanf(graFile, "%d%d%d%d%d", &studentId, &test1, &test2, &test3, &test4);
arr2[i][0] = studentId;
arr2[i][1] = test1;
arr2[i][2] = test2;
arr2[i][3] = test3;
arr2[i][4] = test4;
else
printf("nThis file does not exist.");
return;
printf("n %11s%11s%11s%11s%11s%11s", "Student Id","Test 1","Test 2","Test 3","Test 4","Finaln");
printf("*********************************************************************n");
for(int i = 0; i < x; i++)
for(int j = 0; j < y; j++)
printf("%11.0f", arr2[i][j]);
printf("n");
printf("*********************************************************************n");
fclose(graFile);
return;
That gives me this output
Enter the text filename: grades.txt
Student Id Test 1 Test 2 Test 3 Test 4 Final
*********************************************************************
6814 85 86 92 88 0
7234 76 81 84 78 0
6465 87 54 68 72 0
7899 92 90 88 86 0
9901 45 78 79 80 0
8234 77 87 84 98 0
7934 76 91 84 65 0
7284 56 81 87 98 0
7654 76 87 84 88 0
3534 86 81 84 73 0
*********************************************************************
Now I just need to create a new function that averages the test grades from this array and put it into the last column but I'm having trouble doing that. I appreciate any help I can get.
c arrays function average
c arrays function average
asked Nov 11 at 2:47
Angie
12
12
arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
– rainhaven
Nov 11 at 3:04
Besides the obvious answer above, your program has several issues: 1. if(graFile != NULL) needs to be outside the for loop, not inside. 2. return value of fscanf needs to be checked; unless you're absolutely sure the file always contains at least x rows and the format is always consistent with your fscanf.
– rainhaven
Nov 11 at 3:07
Do you know how I could do that in a function of its own? The file only contains that amount of rows because this is a project for school so it will never be modified. So once I put the if condition outside the loop will my code be okay?
– Angie
Nov 11 at 3:11
A function take a float[x][y] as parameter, where the 1st column is student id, 2-5 columns are scores, the function saves the average to the last (6th) column. Is this you need?
– rainhaven
Nov 11 at 3:21
Yes, exactly that.
– Angie
Nov 11 at 3:24
add a comment |
arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
– rainhaven
Nov 11 at 3:04
Besides the obvious answer above, your program has several issues: 1. if(graFile != NULL) needs to be outside the for loop, not inside. 2. return value of fscanf needs to be checked; unless you're absolutely sure the file always contains at least x rows and the format is always consistent with your fscanf.
– rainhaven
Nov 11 at 3:07
Do you know how I could do that in a function of its own? The file only contains that amount of rows because this is a project for school so it will never be modified. So once I put the if condition outside the loop will my code be okay?
– Angie
Nov 11 at 3:11
A function take a float[x][y] as parameter, where the 1st column is student id, 2-5 columns are scores, the function saves the average to the last (6th) column. Is this you need?
– rainhaven
Nov 11 at 3:21
Yes, exactly that.
– Angie
Nov 11 at 3:24
arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
– rainhaven
Nov 11 at 3:04
arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
– rainhaven
Nov 11 at 3:04
Besides the obvious answer above, your program has several issues: 1. if(graFile != NULL) needs to be outside the for loop, not inside. 2. return value of fscanf needs to be checked; unless you're absolutely sure the file always contains at least x rows and the format is always consistent with your fscanf.
– rainhaven
Nov 11 at 3:07
Besides the obvious answer above, your program has several issues: 1. if(graFile != NULL) needs to be outside the for loop, not inside. 2. return value of fscanf needs to be checked; unless you're absolutely sure the file always contains at least x rows and the format is always consistent with your fscanf.
– rainhaven
Nov 11 at 3:07
Do you know how I could do that in a function of its own? The file only contains that amount of rows because this is a project for school so it will never be modified. So once I put the if condition outside the loop will my code be okay?
– Angie
Nov 11 at 3:11
Do you know how I could do that in a function of its own? The file only contains that amount of rows because this is a project for school so it will never be modified. So once I put the if condition outside the loop will my code be okay?
– Angie
Nov 11 at 3:11
A function take a float[x][y] as parameter, where the 1st column is student id, 2-5 columns are scores, the function saves the average to the last (6th) column. Is this you need?
– rainhaven
Nov 11 at 3:21
A function take a float[x][y] as parameter, where the 1st column is student id, 2-5 columns are scores, the function saves the average to the last (6th) column. Is this you need?
– rainhaven
Nov 11 at 3:21
Yes, exactly that.
– Angie
Nov 11 at 3:24
Yes, exactly that.
– Angie
Nov 11 at 3:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
void setAverage(float arr[y], int x)
for(int i = 0; i < x; i++)
arr[i][5] = (arr[i][1] + arr[i][2] + arr[i][3] + arr[i][4]) / 4;
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
void setAverage(float arr[y], int x)
for(int i = 0; i < x; i++)
arr[i][5] = (arr[i][1] + arr[i][2] + arr[i][3] + arr[i][4]) / 4;
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
up vote
0
down vote
void setAverage(float arr[y], int x)
for(int i = 0; i < x; i++)
arr[i][5] = (arr[i][1] + arr[i][2] + arr[i][3] + arr[i][4]) / 4;
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
up vote
0
down vote
up vote
0
down vote
void setAverage(float arr[y], int x)
for(int i = 0; i < x; i++)
arr[i][5] = (arr[i][1] + arr[i][2] + arr[i][3] + arr[i][4]) / 4;
void setAverage(float arr[y], int x)
for(int i = 0; i < x; i++)
arr[i][5] = (arr[i][1] + arr[i][2] + arr[i][3] + arr[i][4]) / 4;
answered Nov 11 at 3:47
rainhaven
1403
1403
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
Thank you so much! Quick question though, where would I add my printf statement at?
– Angie
Nov 11 at 4:08
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
You can use printf as in your original code.
– rainhaven
Nov 11 at 4:38
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245419%2fhow-can-i-take-the-average-of-only-certain-rows-and-columns-of-a-2d-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
arr2[i][5] = (test1 + test2 + test 3 + test4) / 4;
– rainhaven
Nov 11 at 3:04
Besides the obvious answer above, your program has several issues: 1. if(graFile != NULL) needs to be outside the for loop, not inside. 2. return value of fscanf needs to be checked; unless you're absolutely sure the file always contains at least x rows and the format is always consistent with your fscanf.
– rainhaven
Nov 11 at 3:07
Do you know how I could do that in a function of its own? The file only contains that amount of rows because this is a project for school so it will never be modified. So once I put the if condition outside the loop will my code be okay?
– Angie
Nov 11 at 3:11
A function take a float[x][y] as parameter, where the 1st column is student id, 2-5 columns are scores, the function saves the average to the last (6th) column. Is this you need?
– rainhaven
Nov 11 at 3:21
Yes, exactly that.
– Angie
Nov 11 at 3:24