Practice program (using arrays....something I don't quite understand) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Practice program (using arrays....something I don't quite understand)

Hello all, my C++ instructor gave us a practice program to write (in preparation for our upcoming exam). It involved arrays. I am awful at C++ and don't know where to begin. If anyone could give me any pointers or hints or a little code; that would be fantastic!

Thank you!


Here's the beginning 2 steps:

1. Declare an array to hold 200 integers (which represent scores) and fill it with random values in the range of 50 to 100. Then place the value 100 in the very first array element and the value 45 in the very last. Implement this as a function.

2. Display the elements of the array, 20 per line with space between each number. Each number should take up the same amount of space (the same width) on the screen Write a second function to do this. It should begin by displaying a title: "The Array". Hint: as you go through the array, displaying the elements, check the each subscript before displaying the element - if the subscript is a multiple of 20, output an endl first.

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : Hello all, my C++ instructor gave us a practice program to write (in preparation for our upcoming exam). It involved arrays. I am awful at C++ and don't know where to begin. If anyone could give me any pointers or hints or a little code; that would be fantastic!
    :
    : Thank you!
    :
    :
    : Here's the beginning 2 steps:
    :
    : 1. Declare an array to hold 200 integers (which represent scores) and fill it with random values in the range of 50 to 100. Then place the value 100 in the very first array element and the value 45 in the very last. Implement this as a function.
    :
    : 2. Display the elements of the array, 20 per line with space between each number. Each number should take up the same amount of space (the same width) on the screen Write a second function to do this. It should begin by displaying a title: "The Array". Hint: as you go through the array, displaying the elements, check the each subscript before displaying the element - if the subscript is a multiple of 20, output an endl first.
    :
    :


    Take it one small step at a time -- do not get overwhelmed by the amount of words in the assignment. Do one sentence at a time and you will probably be ok.

    For example: the first sentence reads "Declare an array to hold 200 integers". That means something like this:
    [code]
    int array[200];
    [/code]

    The next sentence wants you to use rand() function to generate random number in each of the 200 integers of that array. write a loop and put rand() inside that loop. Don't try to do on with the assignment until you get this much done and working correctly. Then do on to the next sentence in the assignment.



  • bilderbikkelbilderbikkel Posts: 754Member
    (As the tradition dictates) Check
    www.codepedia.com/1/CppFunction
    www.codepedia.com/1/CppArray
    www.codepedia.com/1/CppCout
    www.codepedia.com/1/CppRand

    See ya,


    bilderbikkel

  • sph_grad2003sph_grad2003 Posts: 9Member
    Thank you both! Those tips helped. I just need to take it one step at a time and I think I'll be ok! Thanks, again :)
  • bluj91bluj91 Posts: 133Member
    [b][red]This message was edited by bluj91 at 2006-11-15 15:30:40[/red][/b][hr]
    : (As the tradition dictates) Check
    : www.codepedia.com/1/CppFunction
    : www.codepedia.com/1/CppArray
    : www.codepedia.com/1/CppCout
    : www.codepedia.com/1/CppRand
    :
    : See ya,
    :
    :
    : bilderbikkel
    :
    :

    To get the rand() to be really random you must set the seed using srand(). I usually set this to the return value of time(NULL).

    ie.

    [code]
    #include
    #include

    using namespace std;

    int main(int argc, char ** argv)
    {
    srand(time(NULL));
    rand();
    ...//what ever is next
    }
    [/code]

    You can also exact an arrange on rand() by using the modulus operator, %, which returns the remainder of an integeral division.

    ie.

    [code]
    #include
    #include

    using namespace std;

    #define __RANDMIN__ 50
    #define __RANDMAX__ 100

    int main(int argc, char **argv)
    {
    srand(time(NULL));
    rand() % __RANDMAX__ + __RANDMIN__; // to generate a random number between __RANDMIN__, 50, and __RANDMAX__ 100
    //what ever goes next
    }
    [/code]

    The only tricky thingy with this if you want to generate really big random number or negative random numbers.

    Last of all, the tricky thing I found when I first started out with arrays is that you access the element of an array using 0, not 1. And also to declare an array of five elements you use 5, not 4.

    ie.

    [code]
    #include

    using namespace std;

    int main(void)
    {
    int a[5] = {0,10,20,30,40};
    cout << a[4]; //would the fifth element, 40, not the forth element, 30.
    return 0;
    }

    COol


Sign In or Register to comment.