Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 518
  • Last Modified:

C++ Console App

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
December2000
Asked:
December2000
  • 3
  • 2
2 Solutions
 
jkrCommented:
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
 
jkrCommented:
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
 
December2000Author Commented:
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
 
jkrCommented:
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
 
December2000Author Commented:
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now