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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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 programming language. Basically, the code will work like this, it will read the txt file "Number.txt", then convert any value less than 0 to a 0 in that specific box, It will then specify what boxes/locations have a 0 value, it will also display a total amount of boxes that had a value less than 0, then display the final matrix/number txt, with the negative values converted to 0's 

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
Transcribed Image Text:### 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
**Directory Structure and Data File Explanation**

Welcome to the Module 12 homework section. Please find below the contents and associated data information which highlight key files and directories you'll be working with.

**Directory and File Overview:**

In the left pane, we have the directory structure, which includes the following items:

- **Module 12 hw** (Main Directory)
  - **Module 12 hw** (Sub Directory)
    - **main.c** (C Programming Source File)
    - **Number.txt** (Text File containing Numerical Data)
    - **sailor.txt** (Text Data File)
    - **Distribution.txt** (Text Data File)
  - **Products** (Sub Directory)

**Contents of Number.txt:**

The right pane displays the contents of the file **Number.txt** with the following numerical data organized in rows and columns:

|   | Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 |
|---|----------|----------|----------|----------|----------|----------|
| 1 | 1.54     | -0.35    | 1.67     | 1.89     | 0.90     |
| 2 | 2.83     | 4.03     | -0.21    | -0.15    | 0.76     |
| 3 | -1.26    | 1.73     | 1.72     | -0.07    | 1.32     |
| 4 | 1.86     | 0.94     | 2.63     | 0.19     | 1.31     |
| 5 | 1.32     | 1.71     | 1.49     | -1.94    | 0.14     |

This numerical data may be used for various computational analyses, such as statistical analysis, numerical modeling, or data visualization exercises. Each row and column can represent different variables or collected data points.

**Instructions:**

1. Ensure you understand the directory structure to locate respective files.
2. Use the contents of **Number.txt** for your data analysis tasks.
3. Refer to **main.c** for the associated C programming tasks where you will implement computations or algorithms based on the data provided.

If you have any questions or need further assistance, please consult the related modules or reach out
Transcribed Image Text:**Directory Structure and Data File Explanation** Welcome to the Module 12 homework section. Please find below the contents and associated data information which highlight key files and directories you'll be working with. **Directory and File Overview:** In the left pane, we have the directory structure, which includes the following items: - **Module 12 hw** (Main Directory) - **Module 12 hw** (Sub Directory) - **main.c** (C Programming Source File) - **Number.txt** (Text File containing Numerical Data) - **sailor.txt** (Text Data File) - **Distribution.txt** (Text Data File) - **Products** (Sub Directory) **Contents of Number.txt:** The right pane displays the contents of the file **Number.txt** with the following numerical data organized in rows and columns: | | Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 | |---|----------|----------|----------|----------|----------|----------| | 1 | 1.54 | -0.35 | 1.67 | 1.89 | 0.90 | | 2 | 2.83 | 4.03 | -0.21 | -0.15 | 0.76 | | 3 | -1.26 | 1.73 | 1.72 | -0.07 | 1.32 | | 4 | 1.86 | 0.94 | 2.63 | 0.19 | 1.31 | | 5 | 1.32 | 1.71 | 1.49 | -1.94 | 0.14 | This numerical data may be used for various computational analyses, such as statistical analysis, numerical modeling, or data visualization exercises. Each row and column can represent different variables or collected data points. **Instructions:** 1. Ensure you understand the directory structure to locate respective files. 2. Use the contents of **Number.txt** for your data analysis tasks. 3. Refer to **main.c** for the associated C programming tasks where you will implement computations or algorithms based on the data provided. If you have any questions or need further assistance, please consult the related modules or reach out
Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY