Module 12 hw ) Z My Mac Finished running Module 1 Q A D c main.c Number.txt Module 12 hw) Module 12 hw) Number.txt No Selec Module 12 hw Module 12 hw 1 1.54 -0.35 1.67 1.89 0.90 2 2.83 4.03 -0.21 -0.15 0.76 c main.c -1.26 1.73 1.72 -0.07 1.32 Number.txt 4 1.86 0.94 2.63 0.19 1.31 sailor.txt 1.32 1.71 1.49 -1.94 0.14 6 Distribution.txt 7 Products 8
I need some help redoing some code, I have a setup code that works for the problem, but I wouldd like to see if there is any other way of doing the code, in C
Here is the code that works but I would like to be changed,
#include <stdio.h>
#include <stdlib.h>
int main(){
int i=0,j=0;
int row = 0,col = 5;
float num1,num2,num3,num4,num5;
FILE *fptr;
if ((fptr = fopen("Number.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
while(fscanf(fptr,"%f %f %f %f %f", &num1,&num2,&num3,&num4,&num5) !=EOF){
row++;
}
fclose(fptr);
float arr[row][col];
float arr2[row][col+1];
float sum = 0;
int count = 0;
if ((fptr = fopen("Number.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
i = 0;
while(fscanf(fptr,"%f %f %f %f %f", &num1,&num2,&num3,&num4,&num5) !=EOF){
arr[i][0] = num1;
arr[i][1] = num2;
arr[i][2] = num3;
arr[i][3] = num4;
arr[i][4] = num5;
i++;
}
fclose(fptr);
printf("\nNumber.txt: ");
for(i=0;i<row;i++){
printf("\n%.2f %.2f %.2f %.2f %0.2f", arr[i][0],arr[i][1],arr[i][2],arr[i][3],arr[i][4]);
}
printf("\n");
for(i=0;i<row;i++){
sum=0;
for(j=0;j<col;j++){
if(arr[i][j]<0){
arr2[i][j] = 0;
count++;
printf("\nRow %d Col %d cell has less than zero values",i,j);
}
else {
arr2[i][j] = arr[i][j];
}
sum+=arr2[i][j];
}
arr2[i][j] = sum;
}
printf("\n\nTotal %d cells has less than zero values",count);
printf("\n\nNumber2.txt: ");
for(i=0;i<row;i++){
printf("\n%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f\n", arr2[i][0],arr2[i][1],arr2[i][2],arr2[i][3],arr2[i][4],arr2[i][5]);
}
FILE *fp;
if ((fp = fopen("Number2.txt","w")) == NULL){
printf("Error! creating file");
exit(1);
}
for(i=0;i<row;i++){
fprintf(fp,"%.2f %.2f %.2f %.2f %.2f %.2f\n", arr2[i][0],arr2[i][1],arr2[i][2],arr2[i][3],arr2[i][4],arr2[i][5]);
}
return 0;
}
Please only use C programming, and try to use similar code lines to what I have, I just want to see if there is another way of doing this, attached is the number file I would like to be scanned and what the output of the code should look like.
![### Working with Files in C and Analyzing Data
In this example, we demonstrate how to read from a file, process the data, and analyze the content using a simple C program.
#### Code Explanation:
Here is the main part of the C program that opens a file named `Number.txt`, reads its content, and processes it:
```c
#include <stdio.h>
FILE *fptr;
if ((fptr = fopen("Number.txt", "r")) == NULL) {
printf("Error! opening file");
exit(1);
}
while (fscanf(fptr, "%f %f %f %f %f", &num1, &num2, &num3, &num4, &num5) != EOF) {
row++;
}
fclose(fptr);
float arr[row][col];
float arr2[row][col+1];
float sum = 0;
```
1. **File Opening and Reading:**
- The program opens `Number.txt` in read mode.
- It scans each line of the file, extracting five floating-point numbers into variables (`num1`, `num2`, `num3`, `num4`, `num5`).
- It keeps track of the number of rows read.
2. **Data Storage:**
- The data is stored in a 2D float array named `arr`.
- Another array `arr2` is used for further processing.
- A variable `sum` is initialized to zero, presumably to accumulate the total of certain operations.
#### Sample Output Analysis
The lower section of the output console contains processed data and results:
```
Number.txt:
1.54 -0.35 1.67 1.89 0.90
2.83 4.03 -0.21 -0.15 0.76
-1.26 1.73 1.72 -0.07 1.32
1.86 0.94 2.63 0.19 1.31
1.32 1.71 1.49 -1.94 0.14
Row 0 Col 1 cell has less than zero values
Row 1 Col 2 cell has less than zero values
Row 1 Col 3 cell has less than zero values
Row 2 Col 0 cell has less than zero values
Row 2 Col 3](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F986a0f43-c508-42df-9c7a-5fee8dc0775f%2F118b0267-7d4e-49f6-a2d0-0f7d7ca76e82%2Fboufnfq_processed.png&w=3840&q=75)


Step by step
Solved in 2 steps with 3 images









