GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-Apr-2007, 15:54
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

Help with an Array problem


I'm doing this assignment. Just so it gets explain thoroughly, here's the assignment what I've coded: Write a C++ program to input ten integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int findMax(int [], int);  

int main()
{
  const int NUMPTS = 10;
  int i, nums[NUMPTS], total = 0;
}
{
cout << "Enter a number: "; >> nums[i]
}
{  
  cout << "\nThe maximum value is "
       << findMax(nums,NUMPTS) << endl;
       
  return 0;
}
int findMax(int vals[], int numels)
{
  int i, max = vals[0];
  
  for (i = 1; i < numels; i++)
    if (max < vals[i]) max = vals[i];
    
  return max;
}
The errors I am receiving are:
Code:
E-8-1-8a.cpp:11: error: syntax error before `{' token
I don't know why I am getting this error. I'm not even sure if I'm doing this program correctly because I am unable to get the program to run. Any suggestions or advice would be helpful.
  #2  
Old 05-Apr-2007, 17:05
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with an Array problem


Quote:
Originally Posted by silverneon
The errors I am receiving are:
Code:
E-8-1-8a.cpp:11: error: syntax error before `{' token
I don't know why I am getting this error. I'm not even sure if I'm doing this program correctly because I am unable to get the program to run. Any suggestions or advice would be helpful.
Here are your lines 1-10:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int findMax(int [], int);  

int main()
{
  const int NUMPTS = 10;
  int i, nums[NUMPTS], total = 0;
}

That is the entire main() function. Open brace - function - close brace. Look for examples in your book or other course materials. They don't put braces around groups of statements just for the heck of it!

In your program: Put an open brace '{' on the next line after main()
Put a close brace '}' on the next line after return 0; No other braces are needed in your main() function. The function findMax is properly braced, but you may still have an error or two to fix.


Oh, by the way: no one was born knowing this stuff. Keep plugging! If you get messages or other behavior that you don't understand, ask again.

Thanks for posting.

Regards,

Dave
  #3  
Old 05-Apr-2007, 19:28
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

Re: Help with an Array problem


Okay I fixed the program and it runs but I get this:

CPP / C++ / C Code:
#include <iostream>
using namespace std;

int findMax(int [], int);  

int main()
{
  const int NUMPTS = 10;
  int i, nums[NUMPTS], total = 0;

cout << "Enter a number: " << nums[i] << endl;
cout << " The maximum value is " << findMax(nums,NUMPTS) << endl;
       
  return 0;
}

int findMax(int vals[], int numels)
{
  int i, max = vals[0];
  
  for (i = 1; i < numels; i++)
    if (max < vals[i]) max = vals[i];
    
  return max;
}
Code:
Enter a number: 0 The maximum value is 69176
It's supposed to allow the user to enter 10 numbers then display the maximum number that was entered.
  #4  
Old 05-Apr-2007, 20:10
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with an Array problem


Quote:
Originally Posted by silverneon
It's supposed to allow the user to enter 10 numbers then display the maximum number that was entered.

What part of your program expects the user to enter 10 numbers?


You ask for a number (one time), then try to store it in an undefined memory location. Why do I say undefined? You haven't given a value to i, and then you try to store something in nums[i]. What do you expect to happen?

Read your course materials. If you want to do something 10 times, one way is to make a loop that is traversed 10 times. If you want to get 10 numbers from the user and store them into array elements nums[0], nums[1], ... nums[9] you would make a loop that lets i go over the range [ 0, 9 ] and, each time through the loop, get a number from the user and store it in nums[i]. Something like that.

Surely your course materials have examples of for(){}loops. Right?

Regards,

Dave
  #5  
Old 05-Apr-2007, 22:59
silverneon silverneon is offline
New Member
 
Join Date: Mar 2007
Posts: 12
silverneon is on a distinguished road

Re: Help with an Array problem


I got the program to loop but it does this:
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int findMax(int [], int);  

int main()
{
  const int NUMPTS = 10;
  int i, nums[NUMPTS], total = 0;
  for (i = 0; i < NUMPTS; i++)
  cout << "Enter a number: " << nums[NUMPTS] << endl;
  cout << "The maximum value is " << findMax(nums,NUMPTS) << endl;
       
  return 0;
}

int findMax(int vals[], int numels)
{
  int i, max = vals[0];
  
  for (i = 1; i < numels; i++)
    if (max < vals[i]) max = vals[i];
  return max;
}
Code:
Enter a number: 0 Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: 4 Enter a number: 5 Enter a number: 6 Enter a number: 7 Enter a number: 8 Enter a number: 9 The maximum value is 69208
I'm only supposed to use one loop though. I'm doing my best to understand this programming.
Last edited by admin : 05-Apr-2007 at 23:39. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #6  
Old 06-Apr-2007, 07:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,710
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with an Array problem


Quote:
Originally Posted by silverneon
I got the program to loop but it does this:

Code:
Enter a number: 0 Enter a number: 1 Enter a number: 2 Enter a number: 3 Enter a number: 4 Enter a number: 5 Enter a number: 6 Enter a number: 7 Enter a number: 8 Enter a number: 9 The maximum value is 69208
Well when I run it, I get:
Code:
Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 Enter a number: 2280648 The maximum value is 1628859000

The program ran to completion before giving me a chance to enter anything. The program doesn't have any input statements, so how could it possibly work? It is pretty hard to help unless you post the program that you are actually running.

A way to get user input is with cin:

CPP / C++ / C Code:
    int x;

    cout << "Enter an integer: ";
    cin  >> x;

Aren't there any examples in your book or other course materials?

Quote:
Originally Posted by silverneon
I'm only supposed to use one loop though.
Then do everything in one loop. Do you really need to put the entries into an array? You could just get user input in a loop, and use the logic of your findMax function to inspect the input values each time. I have no way of knowing what you are really supposed to do.

If you really want to put the values into an array you can do it in the same loop, but I don't see the point. Bottom line: if your assignment requires that the entire program must have only one loop, you can't have one loop to get user input and another loop to find the maximum of the input values.

Quote:
Originally Posted by silverneon
I'm doing my best to understand this programming.

Then I respectfully request that you look at examples in your book or other course materials.

What part do you not understand? You have done the logic for finding max.


Code:
1. Declare int variables x and max 2. Print message to user. 3. Get user input into "max" 4. Make a loop that does the following nine times: LOOP Get user input into x IF x is greater than max, then set max equal to x END IF END LOOP 5. Print out the value of max

Regards,

Dave
Last edited by davekw7x : 06-Apr-2007 at 08:34.
 
 

Recent GIDBlogMeeting the populace by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with filling a char array in a windows GUI app. ryver C++ Forum 1 13-Aug-2006 08:22
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31
problem modifying an array of char in a function ronin C Programming Language 10 28-Mar-2005 18:15
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
problem reading to a dynamic array noamfrie C Programming Language 9 02-Jan-2005 18:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.