How can I take the average of only certain rows and columns of a 2D array?












0














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.










share|improve this question






















  • 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?
    – something
    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.
    – something
    Nov 11 at 3:24
















0














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.










share|improve this question






















  • 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?
    – something
    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.
    – something
    Nov 11 at 3:24














0












0








0







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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 2:47









something

43




43












  • 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?
    – something
    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.
    – something
    Nov 11 at 3:24


















  • 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?
    – something
    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.
    – something
    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?
– something
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?
– something
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.
– something
Nov 11 at 3:24




Yes, exactly that.
– something
Nov 11 at 3:24












1 Answer
1






active

oldest

votes


















0














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;
}
}





share|improve this answer





















  • Thank you so much! Quick question though, where would I add my printf statement at?
    – something
    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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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;
}
}





share|improve this answer





















  • Thank you so much! Quick question though, where would I add my printf statement at?
    – something
    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
















0














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;
}
}





share|improve this answer





















  • Thank you so much! Quick question though, where would I add my printf statement at?
    – something
    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














0












0








0






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;
}
}





share|improve this answer












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;
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 3:47









rainhaven

1403




1403












  • Thank you so much! Quick question though, where would I add my printf statement at?
    – something
    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?
    – something
    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?
– something
Nov 11 at 4:08




Thank you so much! Quick question though, where would I add my printf statement at?
– something
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


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini