Solved

C++ Console App

Posted on 2013-01-20
5
510 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
strCopies  challenge 17 74
groupSum6 challenge 6 48
SQL400 max size 5 57
Basic Java Case or If-Else statement... 3 13
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now