Page 1 of 2 12 LastLast
Results 1 to 10 of 20
  1. #1
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30

    vtm Video array and loop Question

    Hi all just been watching the first set vtm and was thinking about doing this as surgested
    /*Assignment One: Averaging Values
    For this assignment you will create an Integer array that holds 10 RANDOM values.
    Create a loop to find the average of those 10 values and print the result.
    */

    How would this be done tho using what I have learn from the first vtm would it be something like this

    //We nee this to use count and cin
    #include <iostream>

    //Create the main function
    int main(){

    int myArray[10];

    array1[0] = 10;
    array2[1] =20;
    so on so on untill i get to the last one
    array10[9] =100;

    }

    is that right then how would I do a loop to add all the number up?

  2. #2
    Join Date
    Mar 2004
    Posts
    0
    do you know for or while loops?
    first you would create an array with 10 int elements. then you would loop though each one and assign a value from the rand() function. after of which you would loop though them again, adding up an int with the value from each array element. the last thing to do would be dividing by 10... then you have your avarage...
    what spacific things do you have questions about?
    good luck
    ~

  3. #3
    Join Date
    Aug 2004
    Posts
    6
    I think you meant to say:

    Code:
    myArray[0] = 10
    myArray[1] = 20
    ... so on
    And as for random variables, if they didn't teach you how to use the random function I guess that's the way you would put in 'random' values into the the array. I say this because I have not watched the C++ VTMs, and as for your for loop, I'm just going to help you out a bit and then you can figure out the rest.

    Code:
    #include <iostream>
    
    int main(void)
    {
        // myArray has be assigned values
    
        for (int i = 0; i < 10; i++)
        {
            // put in code here to take the average, 
            // make use of the 'i' variable
        }
    
        return 0;
    }

  4. #4
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    Hi I dont know the rand function and I do understand while and for.I am having trouble understanding how I use a loop to add all myArray's and then divide them also would my array all have to be differnt names.

    #include <iostream>

    int main(void)
    {
    // myArray has be assigned values

    myArraya[0] = 10;
    myArrayb[1] = 20;
    myArrayc[2] = 30;
    myArrayd[3] = 40;
    myArraye[4] = 50;
    myArrayf[5] = 60;
    myArrayg[6] = 70;
    myArrayh[7] = 80;
    myArrayi[8] = 90;
    myArrayj[9] = 100;
    myArrayk[10] = 110;

    I dont understand how the loop would be written using the above information also if I was not using a loop could I do it like this (I know this is a longer way of doing it)

    myArraya + my Arrayb + myArrayc so on so on so on =total;

    total / 10 =Average;

    cout <<" The average of myArray's is" << Average <<endl;

    return 0;
    }

  5. #5
    Join Date
    Sep 2004
    Location
    USA
    Posts
    1,529
    That's not how you would want to do it. Every element of your my array should have the same name, otherwise there is no reason to be using an array in the first place. So, name it myArray. Now, how you get to the different elements is by setting it up like this:

    Code:
    myArray[10]  // = arraySize;
    //Or
    int arraySize = 10;
    myArray[arraySize];
    
    ///  Lets do this
    
    int main()
    {
    	int myArray[10];
    	int i;
    	for ( i = 0; i < 10; i++)  // it is offset so 0-9
    	{
    		//can add a cout here//
    		myArray[i] = rand() % 100;
    	}
    That will set each element of myArray from 0 to 9 to each hold a random value. So the first time through 'i' will be 0. The next time through 1, then 2 and on all the why to 9. you can see this if you add a cout inside the loop.

    Code:
    cout << "myArray number["   << i << "]: ";
    Edit

    I fixed it.
    Last edited by Wolfknightly; 03-25-2007 at 05:21 PM.

  6. #6
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    I am still not understnad this

    int myArray[10];
    int i;
    for ( i = 0; i < 5; i++) // it is offset so 0-9
    {
    //can add a cout here//
    myArray[i] = rand() % 100;
    }

    Surley what that is doing is saying i = o if 5 is less than 0 add one so that gonna got like count up 1,2,3,4,5, but what that doing how is that adding up myArray what is 10,20,30,40,50,60,70,80,90,100 ?

    Or is i setting up the 10 differnt myArrays then the next bit saying

    myArray[i](what will have 9 there = a randomd number modual 100 ?

  7. #7
    Join Date
    Sep 2004
    Location
    USA
    Posts
    1,529
    Yeah, lol, my bad. I meant to put in 10 not 5. don't know why that happened.

    Edit

    Also, it is not if 10 is less then 0. It is if "i" is less then 10. So the first time through 'i' is 0 and 0 is less then 10. The next time through 'i' is 1. The last time you use the loop 'i' is 9. Then when 'i' becomes 10 the for loop fails and so ends.

    Remember, the offset means that the first number in the array is not 1 but instead 0, and the last is not 10 but instead 9.
    Last edited by Wolfknightly; 03-25-2007 at 05:35 PM.

  8. #8
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    Ok well I run the code you put and I now understand how that works but how do I then add all the values in myarray and divide them by 10 to get average from running your script I see that it gives each array a random values the values is between 0 and 99 but how do i say add array[0] to array[9] up then divide by 10 = average array
    Last edited by millwalll; 03-25-2007 at 06:05 PM.

  9. #9
    Join Date
    Dec 2005
    Posts
    755
    I think the way I would do it is like follows.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    // function generates our random numbers
    int gen()
    {
        int range = 100;                        // set the range to generate numbers from
        int randNum = (rand() % range) + 1;     // generate the number
    
        return randNum;
    }
    
    // main function
    int main()
    {
        srand((unsigned)time(0));               // seed the generator
        int myArray[10];
        
        /* set the array */
        for(int i = 0; i <= 9; i++)
        {
            myArray[i] = gen();                 // run the gen() function to set a number to the array
            //std::cout << myArray[i] << std::endl;
        }
    
        /* calculate total */
        int total = 0;
        
        for(int j = 0; j <= 9; j++)
        {
            total = total + myArray[j];
        }
    
        /* calculate average */
        int average = total / 10;
    
        std::cout << average << std::endl;
    
        return 0;
    }
    Last edited by Amorak; 03-25-2007 at 06:09 PM.
    "Believe nothing, no matter where you read it, or who said it, no matter if I have said it, unless it agrees with your own reason and your own common sense." ~ Siddhārtha Gautama

  10. #10
    Join Date
    Mar 2007
    Location
    Kent UK
    Posts
    30
    wow Remember I am only begginer and only know very basic I still having trouble understaning it I understand the first bit how it set the array and gives each one a random number

    /* calculate total */
    int total = 0;

    for(int j = 0; j <= 9; j++)
    {
    total = total + myArray[j];
    }
    its this bit I dont understand this bit code does as it saying j = 0 then if 9 is less than or equal to j add one so wont that loop as

    j= 0
    j=1
    j=2
    j=3 so on ?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •