Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE I NEED HELP WITH MY C PROGRAMMING ASSIGNMENT 2 2179 13 1002 992 979 988 1

ID: 3604453 • Letter: P

Question

PLEASE I NEED HELP WITH MY C PROGRAMMING ASSIGNMENT

2
2179
13 1002 992 979 988 1012 1005 971 992 0
251 1133 1058 1072 1100 1032 1101 1028 993 1091 1121 0
3 1000 1000 1001 1002 1000 999 1000 993 0
91 950 950 950 950 950 950 950 950 950 950 0
0
47
14 988 980 979 977 984 971 982 979 0
15 1211 1191 1189 1199 1202 1195 1197 1200 1204 1199 0
482 1002 1007 1001 1002 1014 999 1003 1002 1000 996 0
0

I want to input data file like this into an array and keep track of the numbers. 0 indicates the end of a set of numbers or termination. first line reads number of batches which is 2. Second line reads batch number (2179). First number indicates the name of the first set. Eg; set 13, 251,3,91. The numbers that follow the name are the elements and 0 terminates the set. Another 0 terminates the first batch. Followed by the second batch number(47).  

Explanation / Answer

Here is the code for you:

#include <stdio.h>
int main()
{
    FILE* fp;   //Declares a file pointer.
    char fileName[50];   //Declares a character array to hold the file name.
    printf("Enter the name of the file to read from: ");   //Prompts for file name.
    scanf("%s", fileName);   //Reads the file name into fileName.
    fp = fopen(fileName, "r");   //Opens the specified file in read mode.
    if(fp == NULL)   //If you failed to open the file for any reason.
    {
       printf("Unable to open the file. ");   //Displays the error message.
       return 0;   //Stops program execution.
    }
    int count = 0;   //Maintains a count of number read from the file.
    int array[100];   //Declares an array of 100 integers.
    int number;   //Declares a variable to read each number.
    fscanf(fp, "%i", &number);   //Reads the first number.
    while(!feof(fp) && number != 0)   //Keeps reading till 0 encounters, or till the end of the file.
    {
       array[count] = number;   //Copy the read number into the array.
       count++;   //Increment the count.
       fscanf(fp, "%i", &number);   //Reads the next number.
    }
   
    //Prints the read numbers to the screen.
    for(int i = 0; i < count; i++)
        printf("%i ", array[i]);
    printf(" ");     
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote