manipulating user input in an array

array average,, highest, lowest

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 9627 Views - Last Post: 01 November 2010 - 05:44 PM Rate Topic: -----

#1 jtcoder  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 16-October 10

manipulating user input in an array

Posted 26 October 2010 - 07:44 PM

This is my second attempt at posting here in DIC. This assignment has me completely stumped. I have read the section about arrays in the book that I have many times. I have done extensive net searches trying to find similar code or something that would guide me in the right direction. I am not sure if I need a different loop for each function (ex the sum, the highest, the average). I tried every method that I have learned so far to accept user input and manipulate it to perform calculations and return a result. I will post more code after I can figure out what approach I need to take. I do not know how to declare or initialize the variables in the code and not at the top next to "int main()".

Here is the assignment:
I need a program that will accept user input - stop when user enters (-1) - the program needs to output the average of the numbers entered - the program also has to find the highest and lowest entry. The program has to be written using the given code with the only changes being permitted are at the bottom.

When I ask my instructor a question for my online class that answer is usually "look in the book". The instructor has admitted that the assigned textbook sucks.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double scores [75];
    int counter =-1;
    do
    {
        counter++;
        cout << "Please enter a score (enter -1 to stop): ";
        cin >> scores [counter] ;
    } while (scores [counter] >=0);
    // can not change above here






I do not want the solution given to me. I just need to figure out how to get it.

Is This A Good Question/Topic? 0
  • +

Replies To: manipulating user input in an array

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: manipulating user input in an array

Posted 26 October 2010 - 08:01 PM

Since the sentinel value -1 is stored in the array, the counter variable can be used directly as the limit value in a for() loop that sums the values in the array and finds the min/max value stored. That means you don't have to test for it.

It also will be the denominator in the average calculation. :)
Was This Post Helpful? 1
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: manipulating user input in an array

Posted 26 October 2010 - 08:02 PM

So what code have you written below the special line?
Show it to us and answer these questions
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect? Give details and, ideally, examples.
( e ) What have you already tried to fix it?
Was This Post Helpful? 1
  • +
  • -

#4 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: manipulating user input in an array

Posted 26 October 2010 - 08:18 PM

Well, let's look at the logic needed to complete this program. First, you need to find the sum of the numbers within the array. You can do this one of two ways. You can store each index of the array in a variable, and find the average of the sum of those variables. That is tedious and not very extensible though, so a better solution would be to use the keyword static.

For a quick review of the use of static, here is an external link -> http://www.cprogramm...tickeyword.html
In short, declaring a static variable within a function or loop (such as main, or a for-loop) will only create that variable once and store it's value within memory. So, using this we would iterate through the array and add each value to the static variable. Then, you could use that variable of sums to find the average (Note: Notice that you have a scoped variable counter. It's use in the loop answers your question of how large the array is).

As for the largest and smallest number within the array, you could utilize a for-loop and check if each the currect index contains a value larger than the following index. If the number is larger, store it in a variable. Perform this check through each index of the array, only storing variables that are the largest. You can use the same logic when finding the smallest value.

I have given you much of the logic needed to complete this program, now you need to parse it into code. If you understand the code that was already given to you by your instructor, this should be rather simple. Good luck and let us know if you have any additional questions.

-alias
Was This Post Helpful? 2
  • +
  • -

#5 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: manipulating user input in an array

Posted 26 October 2010 - 08:24 PM

View Postjtcoder, on 26 October 2010 - 06:44 PM, said:

I need a program that will accept user input - stop when user enters (-1).


while (input != -1)

View Postjtcoder, on 26 October 2010 - 06:44 PM, said:

the program needs to output the average of the numbers entered


Loop through the array after they have finished inputting, add the sum and divide by the size of the input.


View Postjtcoder, on 26 October 2010 - 06:44 PM, said:

the program also has to find the highest and lowest entry.


You need an algorithm that can find the lowest and highest. One way is to set the first element in the array to the min, loop through all array if it is less than the min then store it as min. Do the same for max.

Good luck :)
Was This Post Helpful? 1
  • +
  • -

#6 jtcoder  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 16-October 10

Re: manipulating user input in an array

Posted 27 October 2010 - 11:33 PM

Thanks for the input. I think you pointed me in the right direction. I read and re-read the Cprogramming.com page on the static keyword. I understand what is does and why. I do not understand how to use it in a syntax. I have poured over what little bit of code I have all night.
The code compiles, no errors, no output, the loop for the user input work fine.
I have tried moving around the for loops, declaring different functions, and trying to call the function at the end of the program. I know I still need to get highest and lowest portions to work. I understand how the upper half of the program works. I do not know how to translate it into code for the remainder of the program. I am trying to keep in mind that if this was easy everyone would do it.

#include <iostream>

using namespace std;

int main()
{
    double scores [75];
    int counter =-1;
    do
    {
        counter++;
        cout << "Please enter a score (enter -1 to stop): ";
        cin >> scores [counter] ;
    } while (scores [counter] >=0);
    // can not change above here
      double sum_of_scores (scores[75]);                 // I don't know if I need to
     for (counter =-1; counter <75; counter++)           // call a function or just
     {                                                   // use the line of code
         for ( int scores =0; scores<0; scores++)        // scores /counter
     {
          static int sum = sum + scores;
         sum ++;
         cout << sum_of_scores;
     }
     }


  

Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: manipulating user input in an array

Posted 27 October 2010 - 11:50 PM

Okay, let's go through your code line by line.
This line looks like you are trying to declare a function.
double sum_of_scores (scores[75]);


You can't declare a function inside another function. If you wanted to add a prototype for a function before main or place the function before main() you would be breaking a strict reading of the 'no code above this line' instruction. Unless you know the teacher wants to see you using a function in the this program just drop the whole idea of a function for now.

Anyway, that's enough for now. There is more that worries me but for now the function idea is the key question.
Do you absolutely want to write and call a function?
If yes, what you have done will never work. We will need to talk about what functions are and how to write them,
If no, then take all that out of your code.
My simple suggestion is drop the idea of the function but that is your call.
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: manipulating user input in an array

Posted 28 October 2010 - 12:13 AM

Okay, I'm bored waiting for an answer about what you want to do about the function now. You do realise you need to refresh the page to see changes to the page don't you? It won't display them like an instant message system. You have to ask to see the updates.

So let's go through the rest of your code, line by line.
for (counter =-1; counter <75; counter++)


Why does counter start at -1?
Please explain your thinking there.

for (int scores =0; scores<0; scores++)


This loop will run exactly zero times.
You set 'sum' to zero.
Then you say the loop should run as long as 'sum' is less than zero.
Then you increase 'sum' to 1.
So the loop will never run.


static int sum = sum + scores;


Why the time spent reading about the static keyword and why are you using it here?
Please explain your thinking here
EDIT - Ahhhhhh I found the posting #4 from alias120 above. I see now where the static stuff has come from.
Sorry but that was a bad bit of advice.
Here's the important bit of information from the page linked to The keyword static can be used in three major contexts: inside a function, inside a class definition, and in front of a global variable inside a file making up a multifile program.
You are not going to use a function here.
You are not writing a class.
You are not going to use global variables.
'static' is of no use to you. Your time spent researching it was time wasted I am sorry to say.
Remove it from your code and, for the time being, remove it from your mind. It won't be required in this program.

sum ++;


Why are you adding one to 'sum' on this line?
Please explain your thinking here

cout << sum_of_scores;


This line is a double mystery.
1 - If you were trying to call the function 'sum_of_scores' then you have failed to include the argument that function requires.
2 - As discussed in the previous post I have no clear understanding why you are trying to use a function here at all.

Sorry if that is overwhelming. I was hoping to take it at a slower pace but at an hour per line we would have been here too long.

I am sorry to tell you that each and every line of code you have written makes no sense at all to me and won't do anything like what I think you think it will do. Not great news but better now than when you hand it in for marking.

EDIT
BTW Here is your code laid out neatly and with what I imagine was in the cropped off bottom bit restored so the code will compile. Just so others can see some compiling code if they want to add to anything I have said or correct any mistakes I have made.
#include <iostream>

using namespace std;

int main()
{
	double scores [75];
	int counter =-1;
	do
	{
		counter++;
		cout << "Please enter a score (enter -1 to stop): ";
		cin >> scores [counter] ;
	} while (scores [counter] >=0);
	// can not change above here
	// -------------------------------------------- //

	double sum_of_scores (scores[75]);                 // I don't know if I need to
	for (counter =-1; counter <75; counter++)           // call a function or just
	{                                                   // use the line of code
		for (int scores =0; scores<0; scores++)        // scores /counter
		{
			static int sum = sum + scores;
			sum ++;
			cout << sum_of_scores;
		}
	}
	getchar();
	return 0;
}


This post has been edited by janotte: 28 October 2010 - 12:58 AM

Was This Post Helpful? 1
  • +
  • -

#9 Ollie9  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 91
  • Joined: 10-May 10

Re: manipulating user input in an array

Posted 28 October 2010 - 07:21 AM

I think he set the counter to -1 because in the do while loop it increments counter before inputting. So put the counter++; after the prompt for the score.

You don't need to use a do while loop, while loop is sufficient. Also i would create another variable to get the input from the user, then place that value in the array at the current element.

    while (intInput >= 0)
    {
        cout << "Enter number (-1 to stop): ";
        cin >> intInput;   //read number from user
        intNumber[intCount] = intInput; //store that number in the array at the current element
        intCount++; //increment to the next element in the array
    }



And using for loops is a bad idea to loop through the array as it will count the -1 in the sum and you will need another counter variable to see how many numbers they actually entered, your loop assumes that they enter 75 numbers. If the user only enters 5 numbers after pressing -1, it will add garbage to the sum.

Remember, to try to keep your program as simple as possible. Here is probably the simplest solution. Loop through the array until it equals -1, which is when the user has finished entering numbers.

    intCount = 0;
    while (intNumber[intCount] != -1)
    {
        intSum = intSum + intNumber[intCount]; 
        intCount++;      
    }
    cout << intSum;



Heres the full code, try to understand it:
Note: this program is not complete, as it only works when the user presses -1. The sum will be garbage if the user enters 10 numbers. Ill leave it for you to figure out how to fix this :)
#include <iostream>

using namespace std;

int main()
{
    int intNumber[10];
    int intInput;
    int intSum = 0;
    int intCount = 0;
    while (intInput >= 0)
    {
        cout << "Enter number (-1 to stop): ";
        cin >> intInput;
        intNumber[intCount] = intInput;
        intCount++;
    } 
    intCount = 0;
    while (intNumber[intCount] != -1)
    {
        intSum = intSum + intNumber[intCount]; 
        intCount++;      
    }
    cout << intSum;
    return 0;    
}



Goodluck
Was This Post Helpful? 0
  • +
  • -

#10 jtcoder  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 16-October 10

Re: manipulating user input in an array

Posted 30 October 2010 - 02:49 AM

Thanks again to everyone who had offered their advice and posts. I spent several hours trying to get a function to work with no success. I then got brave and installed code::blocks 10.05 and inherited a slew of problems trying to get it to work. Does anyone know how to get code blocks to open 2 windows at the same time? I am going to try the most recent suggestions to get the program to run.
Will update later,
Thanks Again
Was This Post Helpful? 0
  • +
  • -

#11 jtcoder  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 16-October 10

Re: manipulating user input in an array

Posted 31 October 2010 - 07:13 PM

After reviewing Oliies post I see that I have to make so make something clear. The line that says "cannot change above here" means just that. I cannot change that portion of the code to make the assignment easier. It would make things easier to declare everything as double or int. Also being able to use a prototype above main() would make life easier. This assignment was a sick joke by a bored instructor. I have to use the "do while" or I will get a zero for the assignment. I have tried every variation of the for loop that I can think of with no success. Anyone with any other suggestions? Any websites that can offer some good insight?

Sent from my SAMSUNG-SGH-I897 using Tapatalk
Was This Post Helpful? 0
  • +
  • -

#12 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: manipulating user input in an array

Posted 31 October 2010 - 09:08 PM

Let me see your code as is right now.

Quote

The line that says "cannot change above here" means just that. I cannot change that portion of the code to make the assignment easier.
There's no need to change anything above.

Quote

It would make things easier to declare everything as double or int.
Declared as double or int as opposed to? Because you are working with an integer counter and an array of doubles...

Quote

Also being able to use a prototype above main() would make life easier.
How? You don't need functions for this assignment. In fact, writing a function complicates your work. You still have to implement the algorithm. It's just that with a function, you now have to declare, define, and call it properly. That makes things easier?

Quote

This assignment was a sick joke by a bored instructor.
This assignment is trivial. It's made simpler by the fact that a portion of it was already written by your instructor.

Quote

I have tried every variation of the for loop that I can think of with no success.
This is the problem. You aren't logically constructing an answer. You are guessing and hoping it works out. Not a good strategy in programming.
Was This Post Helpful? 1
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: manipulating user input in an array

Posted 31 October 2010 - 09:33 PM

I starting typing some response and then found that the very wise Oler1s had already said exactly what I was going to say.

The key issue here is, as Oler1s rightly says, that "This assignment is trivial.".

I am unclear how you are making such heavy weather of such an easy challenge. I understand that as a beginner it can all be a bit overwhelming but this is very very easy and I am completely confident you have been shown how each part of what you need to works. The key is simply that you don't seem to have a plan.

You do need to see this assignment as an important decision point. If you can't work this out you may well be wasting your time continuing with this course. On the other hand if you can work it out you will have made a break through to becoming a programmer. This is one of those watershed incidents in life. I am confident you can make the breakthrough and write this trivial program. Do you have that belief in yourself?


Let's look at the assignment requirements.

Quote

I need a program that will accept user input - stop when user enters (-1) -

All done for you.

Quote

the program needs to output the average of the numbers entered -

If you had to work out the average of a list of numbers on a piece of paper how would you do it?
Write down exactly how you would do it on paper.
Once you have that plan try to turn it into code.

Leave everything else until later.
Just work this part out for now.

So next time you post show us your written down plan for this part of the assignment and the code trying to implement that plan sitting there nicely under the code you have already been given.

Give it a try.

This post has been edited by janotte: 31 October 2010 - 09:49 PM

Was This Post Helpful? 1
  • +
  • -

#14 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: manipulating user input in an array

Posted 31 October 2010 - 09:54 PM

To augment something Janotte said about you succeeding at this assignment.

I believe programming is to mathematics as writing is to literature. Or something like that. There is an underlying field, and programming is the craft.

Not everyone is going to be a genius programmer, just as not everyone is going to be a genius writer. There are varying levels of ability in the population. Nevertheless, modern society expects writing competence. It expects people to be able to read and write. Maybe not the best reader, maybe not the best writer. But some working fluency. I believe the same is true of programming. The general population is capable of learning.

If you can sit through math classes, and acquire some working math literacy, you can pick up programming basics. I believe this is an absolute given. Unless your mental faculties are so deficient that you cannot participate in the general education system (and it's evident that you are at least an average human), you are capable, with time and effort, of learning to program something practical. The state of programming has advanced these days that average humans can do so.

But it requires the right attitude. It requires an analytical frame of mind along that required in mathematics. Feedback is immediate when you begin programming. Mistakes are made glaringly evident. As a programmer, you should use this to your advantage. Its an immediate sign that you did something wrong, and you should revisit your work, and analyze it, logically again.
Was This Post Helpful? 0
  • +
  • -

#15 jtcoder  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 13
  • Joined: 16-October 10

Re: manipulating user input in an array

Posted 31 October 2010 - 09:59 PM

I guess I will need to produce a couple of observations of my own. I will keep the sentences simple and the words small.
1. Usually when people criticize other people it is usually because they themselves are unsure of what they say.
2. On a site where help is supposed to be available there should not be insults flung at those seeking it.
3. After spending years learning something the tasks usually get easier. So when someone who is only spent a couple of weeks at it has an issue that the resolution seems to have eluded the ones being asked, then that explains the results they have gotten.

Too many words, not enough coding?

This post has been edited by jtcoder: 31 October 2010 - 10:03 PM

Was This Post Helpful? -3
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2