Thread: Array Problem

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Array Problem

    Hey Guys, I'm having trouble with this one project i'm doing..I'm new to Arrays, and I'm trying to figure out where I'm going wrong, but to no avail.

    My task is to ask how many scores will be input, Then using a loop, enter each test score (Must be at least 6), average and find the largest. I'm having trouble with the logistics i guess.. any advice?

    #include <iostream.h>
    #include <stdlib.h>

    main()
    {int Num, Test[What do i put here?], C, Sum=0; float Avg;

    cout<<"\nHow Many Test Scores do you want to enter?- You Must enter at least 6: ";cin>>Num;

    for(C=0;C<=Num;C++)
    {cout<<"\n Enter Score "<<C<<": "; cin>>Test[C];
    Sum=Sum + Test[C];
    }
    Avg=Sum/Num;

    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    I'm proud to say: I think I know the answer to the first question: 5. One less than the number of elements, which is 6, you say?
    Self Learner--patience required

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Get your user input for the number of scores first, then declare the array.

  4. #4
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    ======================
    Declare :

    int *TestScores;
    int num;
    ======================
    Usage :

    cout<<"enter # of scores";
    cin>>num;

    TestScores=new int[num];

    // then TestScores[3]=99;
    ======================

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    ginoitalo is right, you'll have to use memory
    allocation but at the end of the program you'll have to delete it.
    ======================
    Declare :
    int *TestScores;
    int num;
    ======================
    Usage :
    cout<<"enter # of scores";
    cin>>num; //if user types in 10

    TestScores = (int*)malloc(sizeof(int) * num; //<--
    //TestScores will become an array of 10

    //at the end of program
    free(TestScores);

    ======================

  6. #6
    Unregistered
    Guest
    well you could do declare an array of say onethousand and limit size of user input so you don't need to use dynamic memory if you have never used that before. Otherwise, if you have no control over size of user input, then use of dynamic memory is the ticket.

    int array[1000];
    int index = 20000;

    while(index < 6 || index > 1000)
    {
    cout << "You may enter from 6 - 1000 scores. enter number of scores now. " << endl;
    cin >> index;
    }

  7. #7
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Thanks Guys, I appreciate your help.

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, 11: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