Array Problem!

This is a discussion on Array Problem! within the C++ Programming forums, part of the General Programming Boards category; Okay I have an array set up which is: int number[30]; and the array is filled using random numbers. Now ...

  1. #1
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41

    Lightbulb Array Problem!

    Okay I have an array set up which is:

    int number[30];

    and the array is filled using random numbers. Now my question is how would i go about finding the highest value in the array with its array number. Its really boggling my mind and im sure its probably some simple function.

    Anyways any help would be greatly appreiciated!

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    int offset = 0;
    int maxval = 0;
    
    for (int i = 0; i < 32; i++)
    {
         if (number[i] > maxval)
         {
              offset = i;
              maxval = number[i];
         }
    }
    // offset now contains the highest index;
    // maxval now contains the value of the offset;

  3. #3
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    Okay that didnt really work. It just gave out some wierd numbers that came outta nowehre. Im not sure if it was what i did with it when i put it into my program or if u made a mistake but could you take a quick look at my code and see if can find the problem?

    thanx in advance

    Code:
    /****************************************************
              Class Statistics - awclass.cpp
    -----------------------------------------------------
    Using 1D arrays and looping structures, determine the
    failing status of a mark and display the statistics
                         summary
    -----------------------------------------------------
                  Coded by: Andrew Weir
                   November 20th,2002
    ****************************************************/
    /*
    #include<iostream.h>
    #include<iomanip.h>
    #include<conio.h>
    #include<string.h>
    #include<math.h>
    #include<time.h>
    #include<stdlib.h>
    
    struct info
    {
            char userResponse;
            //used to repeat the program
            int iCounter, studentsFailed, studentsPassed, arrayNumber;
            int arrayActual[30], arrayCounter, randomNumber;
            int offset, maxval;
            //iCounter used for the loop in the outputForm function
            //studentsFailed is to count the amount of failing students
            //studentPasses is to count the amount of passing students
            //arrayNumber used for the user input for amount of arrays
            //arrayActual is the actual array holding the random numbers
            //arrayCounter is used in the rndmProcedure function in the for loop
            //randomNumber is used to generate the random numbers
            string firstName, lastName, fullName;
    };
    
    info vars;
    
    void fowlerStatement()
    {
            gotoxy(25,2);
            cout<<"Class Statistics";
            gotoxy(25,3);
            cout<<"~~~~~~~~~~~~~~~~";
            gotoxy(5,5);
            cout<<"This program will ask the user to input the number";
            gotoxy(5,6);
            cout<<"of students in a class while the computer randomly gives";
            gotoxy(5,7);
            cout<<"                 each student a mark.";
            gotoxy(5,10);
            cout<<"This program was written in C++ by: Andrew Weir";
            getch();
    }
    
    void inputForm(info& vars)
    {
            clrscr();
            gotoxy(25,2);
            cout<<"Class Statistics";
            gotoxy(25,3);
            cout<<"~~~~~~~~~~~~~~~~";
            gotoxy(5,5);
            cout<<"Please enter teacher's first name: ";
            cin>>vars.firstName;
            gotoxy(5,7);
            cout<<"Please enter teacher's last name: ";
            cin>>vars.lastName;
            vars.fullName = vars.lastName+", "+vars.firstName;
            gotoxy(5,9);
            cout<<"Please enter the amount of students in the class: ";
            cin>>vars.arrayNumber;
            if(vars.arrayNumber>30)
            {
                    do
                    {       gotoxy(42,10);
                            cout<<"             ";
                            gotoxy(5,10);
                            cout<<"Please enter a number lower than 30: ";
                            cin>>vars.arrayNumber;
                    }
                    while(vars.arrayNumber>30);
            }
    }
    
    void rndmProcedure(info& vars)
    {
            randomize;
            vars.studentsFailed=0;
            vars.studentsPassed=0;
            for(vars.arrayCounter = 0; vars.arrayCounter != vars.arrayNumber; vars.arrayCounter++)
            {
                    vars.randomNumber = random(99)+1;
                    vars.arrayActual[vars.arrayCounter] = vars.randomNumber;
            }
    
            vars.iCounter=0;
            while(vars.iCounter < vars.arrayCounter)
            {
                    vars.iCounter++;
                    if(vars.arrayActual[vars.iCounter] >=50)
                    {
                            vars.studentsPassed++;
                    }
                    if(vars.arrayActual[vars.iCounter] <50)
                    {
                            vars.studentsFailed++;
                    }
            }
    }
    
    void outputForm(info& vars)
    {
            clrscr();
    
    
            ##################PROBLEM#####################
            *********************************************/
            for (int i = 0; i < 32; i++)
            {
                            if (vars.arrayActual[i] > vars.maxval)
                    {
                            vars.offset = i;
                            vars.maxval = vars.arrayActual[i];
                    }
            }
            /***********************************************
            gotoxy(1,1);
            time_t t = time(NULL);
            cout<<ctime(&t);
            gotoxy(25,3);
            cout<<"Class Statistics";
            gotoxy(25,4);
            cout<<"~~~~~~~~~~~~~~~~";
            gotoxy(5,6);
            cout<<"Name of client: ";
            cout<<vars.fullName;
            gotoxy(40,6);
            cout<<"Class Size: ";
            cout<<vars.arrayNumber;
            cout<<setiosflags(ios::right);
            gotoxy(5,8);
            cout<<"Number of failing students.................: ";
            cout<<setw(3)<<vars.studentsFailed;
            gotoxy(5,9);
            cout<<"Number of passing students.................: ";
            cout<<setw(3)<<vars.studentsPassed;
            gotoxy(5,10);
            cout<<"Student number "<<vars.offset<<" obtained a mark of.......: ";
            cout<<setw(3)<<vars.maxval;
            gotoxy(15,14);
            cout<<"-Thank You for using Class Statistics-";
    }
    
    main()
    {
            srand(time(NULL));
            info vars;
            fowlerStatement();
            do
            {
                    inputForm(vars);
                    rndmProcedure(vars);
                    outputForm(vars);
                    gotoxy(15,15);
                    cout<<"Would you like to try again (y/n): ";
                    cin>>vars.userResponse;
            }
            while(vars.userResponse=='y' || vars.userResponse=='Y');
    }
    */

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    79
    Try this.
    Code:
    #include<iostream.h>
    .........
    max=number[0];
    maxposition=0;
    for(int ctr=1; ctr<30; ++ctr)
    {
       if(number[ctr]>max)
       {
          max=number[ctr]; maxposition=ctr;
       }
    }
    cout<<"The maximum value is "<<max;
    cout<<endl<<"Position:"<<(maxposition+1);
    Last edited by sundeeptuteja; 11-22-2002 at 03:20 AM.

  5. #5
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    hmm i tried that last one and it didnt work either
    here is the code i tried it with

    Code:
    #include<iostream.h>
    #include<iomanip.h>
    #include<conio.h>
    #include<string.h>
    #include<math.h>
    #include<time.h>
    #include<stdlib.h>
    
    main()
    {
            int i, number[30], maxposition, max, x, y;
    
            srand(time(NULL));
            randomize;
            i = 1;
            while(i != 30)
            {
                    i++;
                    x = random(99)+1;
                    number[i]=x;
                    cout<<endl<<" "<<number[i];
            }
    
            max=number[0];
            maxposition=0;
            for(int ctr=1; ctr<30; ++ctr)
            {
               if(number[ctr]>max)
               {
                  max=number[ctr]; maxposition=ctr;
               }
            }
            cout<<endl<<"The maximum value is "<<max;
            cout<<endl;
            cout<<"The maximumpos value is "<<maxposition;
            getch();
    }
    isnt there a function or something tyhat could do this job?

  6. #6
    Registered User dizz's Avatar
    Join Date
    Nov 2002
    Posts
    41
    k thanx i got it to work!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM

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