Question

C++ Programming question

Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either

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

Solution:

#include<iostream>

#include<fstream>

#include<stdlib.h>

using namespace std;

//driver code

int main()

{

int n = 0, count, arr[50], i = 0, j, input;

char x[50];

cout << "Enter 0 for user input and Enter 1 for file input: ";

cin >> input;

if(input == 0)

{

cout << "\nHow many numbers? ";

cin >> n;

cout << "Enter " << n << " integers:\n";

for(i = 0; i < n; i++) cin >> arr[i];

//sort the elements

for(i = 0; i < n; i++)

{

for(j = 0; j <= i; j++)

{

if(arr[i] > arr[j])

{

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

//print the elemenets

cout << "\nN\tCount\n";

for(i = 0; i < n; i++){

count = 1;

while(arr[i] == arr[i + 1] && i < n){

count++;

i++;

}

cout << arr[i] << "\t" << count << endl;

}

}

//choose file mode

else if(input == 1)

{

cout << "\nEnter a file name with directory: ";

cin >> x;

ifstream myFile(x);

if( !myFile)

{ // file couldn't be opened

cerr << "Error: file could not be opened" << endl;

exit(1);

}

while(myFile >> arr[i])

{

n++;

i++;

}

//sort data

for(i = 0; i < n; i++)

{

for(j = 0; j <= i; j++)

{

if(arr[i] > arr[j])

{

int temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

//write sorted data to file

ofstream outdata;

outdata.open("output.txt");

if( !outdata )

{ // file couldn't be opened

cerr << "Error: file could not be opened" << endl;

exit(1);

}

outdata << "N\tCount\n";

for(i = 0; i < n; i++){

count = 1;

while(arr[i] == arr[i + 1] && i < n){

count++;

i++;

}

outdata << arr[i] << "\t" << count << endl;

}

outdata.close();

}

else

cout<<"Invalid input!";

return 0;

}

Output with screenshot:

Mode 0:

Online C++ compiler, Online Code C++, compile C++, run C++, and hosty... save run share 6 + new repl 88 talk Sign up Files maMode 1:

Online C++ compiler, Online ... Code C++, compile Ch, run C++, and hosty... save run share + new repl 8a talk Sign up Files iOnline C++ compiler, Online .. - Code C++, compile Cht, run C++, and hosty... save run share 6 + new repl 8a talk Sign up Fil

Please give thumbsup, if you like it. Thanks.

Add a comment
Know the answer?
Add Answer to:
C++ Programming question Problem: 5. Write a program that reads in a list of integers into...
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
  • In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a...

    In C++ Sorted List of User Entered Numbers 2. Write a program that reads in a list of integers into a vector with base type int. Provide the facility to read this vector from an input file. Make sure to ask the user for a filename. The output is a two-column list. The first column is a list of the distinct vector elements. The second column is the count of the number of occurrences for each element. The list should...

  • :Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment...

    :Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment about why coding that way,thanks Problem A: Counting Numbers Problem Description Write a program that reads numbers from the keyboard into an integer array. You may assume that there will be 50 or fewer entries in the array. Your program allows any number of numbers to be entered, up to 50 numbers. The output is to be a two-column list. The first column is...

  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • C Program Question: Write a program that reads all integers that are in the range of...

    C Program Question: Write a program that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a...

  • 1. use c++ write a c++ program that reads 10 integers from a file named input.txt...

    1. use c++ write a c++ program that reads 10 integers from a file named input.txt in this file the 10 integers wil be on a single line with space between the. calculate the average of these numbers and then write his number to a file named output.txt. if the program is unable to successfully complete the file operations then display eero message to the console.

  • write a program in c ++ that reads the length and width of a rectangle from...

    write a program in c ++ that reads the length and width of a rectangle from a file named input.txt and write the area and perimeter of the rectactangle to file named output.txt

  • Using c++ Write a program that reads in a list of up to 25 first names...

    Using c++ Write a program that reads in a list of up to 25 first names from an input file nameData.txt and allows the user to display the name data, sort it in ascending or descending order, count the number of occurrences of a given name, or exit the program. The input file consists of a single names per line with each line terminated with an end of line (i.e. using Enter key to end the line). Your program should...

  • [C++] Write a program that reads a list of integers, and outputs those integers in reverse....

    [C++] Write a program that reads a list of integers, and outputs those integers in reverse. You must use an array for this lab. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 5 To achieve the above, first read the integers into...

  • Write a program that reads a file named input.txt and writes a file that contains the...

    Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

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