Question

In this Assignment assume that you have a text file with numbers separated by newlines. you...

In this Assignment assume that you have a text file with numbers separated by newlines. you want to read the numbers in and add them up. since we want to be flexible on the type and size of numbers we can handle, we will use double floating point.

Text file contains following numbers

8

3

6.5

9

0

assume that the files are well-formed: it will contain only valid numbers separated by newlines.

Using C language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C:

#include<stdio.h>
#include<string.h>
int main()
{
   //Opening input.txt in read mode
   FILE *fp=fopen("input.txt","r");
   //If fp is null this means that failed to open file
   if(fp==NULL)
   {
       printf("Failed to open the file");
       return 0;
   }

   //Initialize a character array to store one line of file
   char num[500];
   //sum stores the final sum of number in file
   float sum=0;
   //Iterate over the file
   while(fgets(num,sizeof(num),fp)!=NULL)
   {
       //Covert string into float
       float x;
       sscanf(num, "%f", &x);
       //add x to sum
       sum=sum+x;
   }
   //close the file
   fclose(fp);
   //Print the result
   printf("Sum is: %f\n",sum);
   return 0;
}

output screenshot:

Add a comment
Know the answer?
Add Answer to:
In this Assignment assume that you have a text file with numbers separated by newlines. you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...

    Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...

  • Hi, i am working with MATLAB. I have a text file from which I have to...

    Hi, i am working with MATLAB. I have a text file from which I have to read in my data. So far i have this in my code. fid = fopen('ecgnormaloff.txt', 'r'); data = textscan(fid,'%f') According to all the websites, this should be enough to read in my points, however, I get this as my output data = 1×1 cell array {0×1 double} >> And when I try to access my data, there is nothing. I do not want to...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • This is a C programming question. We have a file containing floating point numbers (we don't...

    This is a C programming question. We have a file containing floating point numbers (we don't know how many). Write a complete C program that alternately averages only the floating point numbers (for example 3.50, 12.507, 123.60,5.98 would lead to the result (12.507+5.98)/2), ignore the whole numbers. Use file input, assuming the file is named "numbers.data". The program should be compatible with files that contain several numbers on separate lines.

  • Write a class named SatData that reads a JSON file containing data on 2010 SAT results...

    Write a class named SatData that reads a JSON file containing data on 2010 SAT results for New York City and writes the data to a text file in CSV (comma-separated values) format. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named save_as_csv that takes as a parameter a list of DBNs (district...

  • please read directions first. this is supposed to be a hybrid programe add comments please JAVA...

    please read directions first. this is supposed to be a hybrid programe add comments please JAVA please add comments Programming Project 1 and Programming Project 2 "Hybrid" Your goal is to write a program that combines the functionality of both Programming Project 1andProgramming Project 2 into a single program. That is, your program should read a file ( Numbers.txt) of numbers of type double that contains some duplicates and is ordered smallest to largest. Your program should ignore the duplicate...

  • FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data...

    FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...

  • Write a program that reads the scores from the file and displays their total and average.

    Suppose that a text file Lab2.txt contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average.In the text file, the scores are separated by blanks.The program should contain two methods with these signatures (it can contain other methods besides these if you think appropriate):public static double totalScores(File inFile)public static double averageScores(File inFile)The output from the program should look like this successful sample run from my solution:The total of...

  • The question asks you to assume that your input file is an English text that may...

    The question asks you to assume that your input file is an English text that may contain any character that appears on keyboard of a computer. The task is to read each character from the input file and after encrypting it by adding an integer value n (0 < n < 128) to the character, to write the encrypted character to output file. We are supposed to use command line arguments and utilize argc, and argv parameters in main function...

  • Binary files The binary file data.dat contains characters and numbers- it has 16 characters, foll...

    java Binary files The binary file data.dat contains characters and numbers- it has 16 characters, followed by numbers of type int alternating with numbers of type double -so after the first 16 characters, there will be an int, followed by a double, followed by an int, followed by a double, and so on. Write a program to open the file and until the end of file is reached, read in the values in the appropriate data types (that is, read...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT