C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Practice program (using arrays....something I don't quite understand) Posted by sph_grad2003 on 13 Nov 2006 at 5:05 PM
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.

Report
Re: Practice program (using arrays....something I don't quite understa Posted by stober on 13 Nov 2006 at 8:41 PM
: 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:
int array[200];


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.



Report
Re: Practice program Posted by bilderbikkel on 14 Nov 2006 at 12:03 AM
(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

Report
Re: Practice program Posted by sph_grad2003 on 14 Nov 2006 at 8:11 AM
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 :)
Report
Re: Practice program Posted by bluj91 on 15 Nov 2006 at 3:25 PM
This message was edited by bluj91 at 2006-11-15 15:30:40

: (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.

#include <cstdio>
#include <cstdlib>

using namespace std;

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


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

ie.

#include <cstdio>
#include <cstdlib>

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
}


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.

#include <iostream>

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





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.