Arrays

aBefore you even read this, I'm gonna tell you that arrays are completely just confusing me to all extents. Well I need to make a 5 row 10 column array but I'm just competely perplexed. My class is just to big for my teacher to get to us all so if you guys would be so kind as to let me know what I'm doing wrong :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Week 9 programming assignment(arrays).cpp : Defines the entry point for the console application.
//

//  =========
//  Libraries
//  ==================
   #include "stdafx.h"
   #include <stdlib.h>
   #include <iostream>
   #include <iomanip>
//  ==================

//  ==========
//  Namespaces
//  ====================
    using namespace std;
//  ====================

//  =================
//  Constant Integers
//  =============================
    const int NUMBER_OF_ROWS = 5;
    const int NUMBER_OF_COLUMNS = 10;
    const int MIN_RANDOM_NUMBER = 1;
    const int MAX_RANDOM_NUMBER = 50;
//  =================================

//  ===================
//  Function Prototypes
//  ===========================
    int RandomNumber(int, int);
	void RandomNumber( int& );
//  ===========================

//  ===============
//  Function main()
    int main( ) {

//      =====================
//      Variable Declarations
//      =====================
        double alpha[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS];
		int rNumber;

		rNumber = 0;

                for(int row = 0; row < NUMBER_OF_ROWS; row++)
                    for(int col = 0; col < NUMBER_OF_COLUMNS; col++)
                        alpha[row][col] = ( rNumber );

		        for(int row = 0; row < NUMBER_OF_ROWS; row++)
                    for(int col = 0; col < NUMBER_OF_COLUMNS; col++)
						cout << alpha[row][col] << " ";




	return 0;

    }// Function main()
 // ===================

//	=======================
//	Function RandomNumber()
	 int RandomNumber(int min_random_number, int max_random_number) {

		 int rNumber;

		 min_random_number = MIN_RANDOM_NUMBER;
		 max_random_number = MAX_RANDOM_NUMBER;
		 
		 rNumber = (min_random_number < rand() < max_random_number);


      return rNumber;
	  } // Function RandomNumber()
//    ============================
/*
	 void RandomNumber2( ) {

		srand( 1 );

	 }


 */


And the output is
1
2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 Press any key to continue . . . 
Last edited on
This is so ridiculously frusterating :(.
- line 42 does nothing. What are you trying to do there?

- lines 43,44 declare 'row' and 'col' variables. You don't need to declare them again on lines 50,51.

- line 58 prints a single, non-existant, out of bounds element. Accessing alpha[5][10] is bad bad bad because the array is not that big. (you're trying to access the 6th row of an array that only has 5 rows)

- if you want to print the array, you have to loop through it and print each element individually.

- line 69 is all wrong. Since you are returning a comparison (<), it will return either 0 or 1 (false or true). Usually false.

- you're missing a closing brace to close the RandomNumber function
Last edited on
Line 42 I was simply trying to print the array. Deleted it.

lines 43/44 are because it says the variable wasn't be defined but that means I need to set it to 0 right? I run the program and I still get this error.

Line 58 - deleted. I get that now

I'm not sure how to print an array :/. I think that's one of my problems.

Line 69 fixed.

Missing brace fixed.

Edited my new code into original post.

Line 48 is the error you are looking for i guess. alpha[5][10] = (rand(), rand());
This should be something like: alpha[row][col] = (rand(), rand());

Although you might want to use the % operator for getting only random integers smaller than 51 (Like rand()%51). Printing out an array is achieved by simply giving it to cout with a loop. It is like the initialization of the array only with cout .... << alpha[row][col] ... somewhere in it.

You might want to write the for loop like this
1
2
3
4
5
6
               for(; row < NUMBER_OF_ROWS; row++)
                    for(; col < NUMBER_OF_COLUMNS; col++)

                for(int row = 0; row < NUMBER_OF_ROWS; row++)
                    for(int col = 0; col < NUMBER_OF_COLUMNS; col++)


In the first case you will use the variables from lines 42,43. In the second case you delete lines 42,43. You can write it as you like, most people will use the second notation.
Last edited on
i fixed the alpha[5][10] problem thank you but I still get

Run Time Check Failure #3 - the variable "row" is being used without being initialized.
I get that error then
Run Time Check Failure #3 - the variable "col" is being used without being initialized.

How do I use the % operator? Can you give me an example?

I'm really trying hard to learn this stuff.
Sorry i edit my post during you were writing. The edited post should be your solution.

An example is here: http://www.cplusplus.com/reference/clibrary/cstdlib/srand/ And in my post although. Simply use rand()%MAX_NUMBER+1 You might want to read about the modulo operator. It gives you the rest of a division. If you write i = 7%5 the value will be 2.

For you this means you will simply use (rand()%MAX_RANDOM_NUMBER)+1.

rand()%MAX_RANDOM_NUMBER generates values from 0-49 by adding one you get your desired random numbers
Last edited on
Thank you, I edited my post as well to give what my code looks like now but I can't experiment with my stuff because I get those run time error's mentioned above and I am perplexed at how to get rid of them.

I'm going to look at this rand() thing you mentioned while you get back to me. :)
line 42, and 43 are wrong you will have to write a for loop like: for(initializations ; bool-Statement; repeating stuff)

If you use variables, which are not defined already you will create them in the head of the for loop like:

for(int foo =0, int bar = 7000, ... ; code ; code) the lines above should set row and col to 0 like: for(int row = 0, int col = 0; code; code)
Oh got it! Thanks for that. Next problem lol.

I edited it to the point of where I am now, and I need to get the output to NOT be all 0's and to be output in a 5rows 10 columns matrix.
You will have to initialize rand() normally you would initialize it using the actual time. So include #include <ctime> in your file and do only _once_ before using rand()

1
2
/* initialize random seed: */
  srand ( time(NULL) );


This is although in the link above. The rest of your program you will have to figure out on your own. I guess it's not too difficult anymore. You will just have to adjust the ouput loop a little bit. There has to be inserted cout << endl; at some point.
Topic archived. No new replies allowed.