Name this program sum2.c – Using the Data2 file , write a program that allocates
ID: 3689465 • Letter: N
Question
Name this program sum2.c – Using the Data2 file, write a program that allocates an array of rows and columns based on what the user specifies as command line arguments. It should then compute and print the sum of all the elements on the very last row of the array. C programming only please.
DOWNLOAD DATA2 FILE: (wget or curl) troll.cs.ua.edu/cs100/labs/Data2
EXAMPLE INPUT AND OUTPUT FOR PROGRAM:
./a.out 1000 1000
The sum of the numbers on the last row is 48848451
./a.out 2000 500
The sum of the numbers on the last row is 24574952
./a.out 250000 4
The sum of the numbers on the last row is 148908
Explanation / Answer
You should have a file named "DATA.txt" containg the data downloaded.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
int nrow=atoi(argv[1]);
int ncol=atoi(argv[2]);
int array[nrow][ncol];
FILE* myFile;
myFile = fopen("DATA.txt", "r");
int i,j;
long long int sum=0;
for(i=0;i<nrow;++i){
for(j=0;j<ncol;++j){
fscanf(myFile, "%d", &array[i][j]);
if(i==nrow-1)
sum += array[nrow-1][j];
}
}
printf("%lld ",sum);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.