Solved

C++ Console App

Posted on 2013-01-20
5
516 Views
Last Modified: 2013-01-20
Hi Experts!

How would I change this code to ask the users for the numbers of the test set.

#include "stdafx.h"
#include <iostream>

using namespace std;


// Function prototypes
int getMode(int *, int);
int *makeArray(int);

int main()
{

	cout << "This console application determines the mode of a predetermined test set of numbers " << endl;
		
	const int SIZE = 11;
   

	int test[SIZE] = {1, 8, 7, 7, 7, 6, 6, 7, 1, 2, 3};
   
	
	int mode;

	mode = getMode(test, SIZE);
		   
	// Display the mode, if any.
	if (mode == -1)
		cout << "The test set does not have a mode.\n";
	else
		cout << "\nThe mode of the test set is: " 
		     << mode << endl;



	cin.get();
	return 0;
}



int getMode(int *array, int size)
{
	// A pointer for frequencies of each value.
	int *frequencies;
   
	int highest, //  highest frequency
	element, //  element subscript
	count;   // Loop counter

	// Dynamically allocate an array to hold
	// the frequencies of each element in the
	

	frequencies = makeArray(size);
   
	// Store 0 in all the elements of frequencies.

	for (count = 0; count < size; count++)
		frequencies[count] = 0;

	// Find the frequency of each element in array.

	for (int count1 = 0; count1 < size; count1++)
	{
		// The inner loop compares the array element.

		for (int count2 = 0; count2 < size; count2++)
		{
			if (*(array + count2) == *(array + count1))
				(*(frequencies + count1))++;
		}
	
	}


	highest = *frequencies; // Find the element with the highest frequency.
	element = 0;
   
	// find the highest value.
	for (count = 1; count < size; count++)
	{
		if (*(frequencies + count) > highest)
		{
			highest = *(frequencies + count);
			element = count;
		}
	}

	// If there is no mode (no element has a frequency 
	// greater than 1), then return -1. Otherwise return
	// the element with the greatest frequency.
	if (highest == 1)
		return -1;
	else
		return *(array + element);
	
}

// Function makeArray

int* makeArray(int size)
{
	int *ptr = new int[size];

	return ptr;
}

Open in new window

0
Comment
Question by:December2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 38799405
Similar to using 'cout' to output the results, you can use 'cin' (http://www.cplusplus.com/reference/iostream/cin/) to read user input. Since all constraints like arra and array size are given, you basically need a loop up to 'SIZE' to read the individual values, e.g.

// assume the loop counter is 'i'
    cin >> test[i];

Open in new window


I could supply you with the whole loop, but since you are working on an assignment, this would be against EE's rules - and I'm pretty sure you can do that yourself ;o)

See also the tutorial at http://www.cplusplus.com/doc/tutorial/basic_io/ ("Basic Input/Output"), scroll down for the input part.
0
 
LVL 86

Expert Comment

by:jkr
ID: 38799419
Oh, and since this will probably one of the next issues you will encounter, let's cover it right now also - chances are that the user inputs some non-integer values, so the easiest thing is to check that on the fly, i.e.

int read_int(istream& is) {

  int n;

  while (true) {

      is >> n;

      if (is.fail()) { // something went wrong

        cout << "Input was not an integer, please try again" << endl;

        continue; // continue loop
      }

     // input was correct, break out of loop

    return n;
  }

  return 0; // will never get here
}

Open in new window


so that the above would become

// assume the loop counter is 'i'
    test[i] = read_int(cin);
                                            

Open in new window


Well, you might ask why I am passing 'cin' to 'read_int()' in the above, yet the reason is simple - that way, you can use the same code with any open file, not only 'cin'.
0
 

Author Comment

by:December2000
ID: 38799489
Not homework... just want to "optimize or upgrade" my completed assignment code... the requirements were met with the code I posted in the question above...  Thank you :)
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 500 total points
ID: 38799516
Well, then it is just like

  int test[SIZE];
  for (int i = 0; i < SIZE; ++i) {

    test[i] = read_int(cin);

   // or, alternatively, the simple way:
  // cin >> test[i];
  }

Open in new window


;o)
0
 

Author Closing Comment

by:December2000
ID: 38799536
Thank you for the very comprehensive answer! Thank you for the "checking" code also... I will play with this and see what I get :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Progress

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question