Question

Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space....

Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and adds all the integers between those two numbers (starting from the first number and ending at the second. Show your output in B1.txt.

Sample Input:

1 6 18 74 29 38

Sample Output:

21 2622 335

(For further clarification, the first line of the sample output was done by 1+2+3+4+5+6 = 21)

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

/*
 *  C++ Program to calculate sum between all the numbers between two integers
 */

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  int num1, num2, sum;

  fstream ifile, ofile;
  ifile.open("A1.txt", ios :: in);
  ofile.open("B1.txt", ios :: out);

  while (!ifile.eof())
  {
    sum = 0;
    ifile >> num1;
    ifile >> num2;

    for (int i = num1; i <= num2; i++)
    {
      sum += i;
    }

    ofile << sum << endl;
  }

  ofile.close();
  ifile.close();

  return 0;
}

/*  Program ends here */

Add a comment
Know the answer?
Add Answer to:
Consider an input file A1.txt. Each line of A1.txt consists of two numbers separated by space....
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
  • Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space....

    Consider an input file A2.txt. Each line of A2.txt consists of two numbers separated by space. Write a C++ program that reads the two numbers from each line and prints how many numbers are prime between them. Your program must have a user defined function that takes one number as input parameter and detects whether its prime or not. Show your output in a file named B2.txt using the following format: Sample Input: 3 17 42 91 Sample Output: 6...

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • Assume that you will be given a *.txt file, which in the same format of RandSource.txt...

    Assume that you will be given a *.txt file, which in the same format of RandSource.txt (attached). The file has three numbers at each line. Numbers are separated with a space. All number are in the range of [0, 50]. The first two numbers indicate a range. The third one shows number of random numbers to be generated in the range of first two numbers. The range always given in first < second order. If not, raise an error. For...

  • 1. Write a program that reads in two arrays (a1 and a2) of numbers and creates...

    1. Write a program that reads in two arrays (a1 and a2) of numbers and creates a new array (a3) of numbers such that the new array contains all the unique numbers of a1 and a2. Assume the input array elements are unique elements. In the main function, ask the user to enter the length of each array, declare and reads in the numbers for each array, and calculate and display the output array. Example input/output #1: Enter the length...

  • Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of...

    Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or *. The tree represents an arithmetic expression where the value at a non-leaf...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

  • IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following...

    IN JAVA, Knapsack Problem The file KnapsackData1.txt and KnapsackData2.txt are sample input files for the following Knapsack Problem that you will solve. KnapsackData1.txt contains a list of four prospective projects for the upcoming year for a particular company: Project0 6 30 Project1 3 14 Project2 4 16 Project3 2 9 Each line in the file provides three pieces of information: 1) String: The name of the project; 2) Integer: The amount of employee labor that will be demanded by the...

  • Please provide C language code no c++ ,txt file also needed with comment Finish task 5...

    Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...

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