first attempt at array, need help

This is a discussion on first attempt at array, need help within the C++ Programming forums, part of the General Programming Boards category; I am trying to do a problem out of my book that accepts 10 numbers and prints the biggest number ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    120

    first attempt at array, need help

    I am trying to do a problem out of my book that accepts 10 numbers and prints the biggest number using an array. It will always print 0 because thats what my newmax is initialized to. Any help appreciated.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    
    {
        const int NUMEL = 10;
        int fmax[NUMEL];
        int i;
        int max, newmax = 0;
        
          cout << "Enter a number " << endl;
            cin >> max;
        
        for ( i = 0; i <= 9; i++)
        {
            cout << "Enter a number " << endl;
            cin >> fmax[i];
            }
            
            if (fmax[i] > max){
                         
            newmax = fmax[i];
            }
            
        cout << "the biggest number is " << newmax << endl;
    
        
        system ("pause");
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Nevermind guys, I just didnt put the 'if' and newmax inside the foor loop. you can delete this thread

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Random nit-picking:

    Code:
    for ( i = 0; i < NUMEL; i++)
    Will work the same way, and let you use the constant you've defined (rather than hardcoding the loop).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 10:21 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21