Solved

C++ Console App

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert Structure to vector of byte and vice versa 5 41
I could not set window to top 4 63
learn programming 8 95
batch file or script 4 66
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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…

751 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