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...

  • 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

  • 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...

  • hi, please help in c++. I dont understand how to do this, and a lot of...

    hi, please help in c++. I dont understand how to do this, and a lot of the ways on the internet is confusing me, i am a beginner. with all steps and explantions, will rate! Write a program that determines the frequency of each char in an input file. Your program should . read in an unknown number of chars from an input file named input.txt, • using a function written by you, determine the frequency of occurrence of each...

  • ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code...

    ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...

  • How to write a Java program that reads the file "input.txt" and writes all even values...

    How to write a Java program that reads the file "input.txt" and writes all even values from this file into a new file called "output.txt." Sample input 10 12 1 3 5 34 2 5 7 9 44

  • Problem: Write a short C++ program that gets the side of a cube and the radius...

    Problem: Write a short C++ program that gets the side of a cube and the radius of a sphere from the keyboard and writes to a file the surfaces of these shapes.             ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79 points) Algorithm solution (in pseudocode): To accomplish this task, your program will have to take the following steps: 1. Declare a variable named outFile that represents an output stream. 2. Declare a...

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