SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Programming Projects
Question:5 | ISBN:9780321531346 | Edition: 7

Question

Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or

from a file, at the user’s option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are.

The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of

occurrences of each element. The list should be sorted on entries in the

first column, largest to smallest.

CHAPTER 7 / Arrays

For example, for the input

-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

the output should be

N Count

4 2

3 3

2 2

1 4

-1 1

-12 4

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void readFromKeyboard(int [], int&);
void readFromFile(int [], int&);
void Sort(int [], int);
void printValues(const int [], int);
void printOccurrenceResult(const int [], int);

int main()
{ 

	const int MAX_INTEGERS = 50;
	int integersArray[MAX_INTEGERS];
	int entries;
	int numberOfEntries;
	int userChoice;
	cout << "Enter the number of entries: ";
	cin >> entries;
	while(entries < 0 || entries >= 50)
	{
		cout << "Enter the number of entries fewer than 50: ";
		cin >> entries;
	} 
	numberOfEntries = entries;
	cout << "\n1. Read Integers from the keyboard" << endl;
	cout << "2. Read Integers from a file" << endl;
	cout << "Enter your choice: ";
	cin >> userChoice;
	while(userChoice != 1 && userChoice != 2)
	{
		cout << "Enter choice (1 or 2 only): ";
		cin >> userChoice;
	} 
	if(userChoice == 1)

		readFromKeyboard(integersArray, entries);
	else 

		readFromFile(integersArray, entries);
	printValues(integersArray, entries);
	Sort(integersArray, entries);

	printValues(integersArray, entries);

	printOccurrenceResult(integersArray, entries);
	system("pause");
	return 0;
} 
void readFromFile(int integers[], int &n)
{

	string inFileName;
	ifstream inFile;

	cout << "\nEnter a file name: ";
	cin >> inFileName;

	inFile.open(inFileName);
	while(inFile.fail())
	{
		cout << inFileName << " file is not opened or found." << endl;
		cout << "Enter a current file name: ";
		cin >> inFileName;
		inFile.open(inFileName);
	} 

	int i = 0;	
	while(inFile >> integers[i] && i < n)
	{
		i++;
	}
	if(i == 0)	
	{
		cout << "The file contains no numbers that is the file is empty." << endl;	
		system("pause");
		exit(1);
	}
	else if(i != n)
	{
		cout << "The file contains less than " << n << " numbers." << endl;	
		n = i;
	}
}
void Sort(int integers[], int n)
{
	for(int i = 0; i < n - 1; i++)
	{
		for(int j = i; j < n; j++)
		{
			if(integers[i] < integers[j])
			{
				int temp = integers[i];
				integers[i] = integers[j];
				integers[j] = temp;
			} 
		} 
	} 
} 
void printValues(const int integers[], int n)
{
	cout << "\nThe integers stored in the array:" << endl;
	for(int i = 0; i < n; i++)
		cout << integers[i] << " ";
	cout << endl;
}

 

0 0

Discussions

Post the discussion to improve the above solution.