ARRAY Help

hello everyone I am currently working on a project involving multidimensional arrays. The problem is i try to set it to output 50 characters width wise and 20 characters height wise but i seem to come up witha 10 character wide, by like around 60 character high output
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
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <conio.h>
//#include <conio2.h>

using namespace std;

int main(int argc, char *argv[])
{
    int num, width=50, height=20;
    char screen[width][height];
    char spaces;
    for(int i=0; i<width-1; i++)
    {
     for(int j=0; j<height-1; j++)
     {
     num=rand()%100+1;
     if(num==10 || num==20 || num==30 || num==40 || num==50 || num==60 || num==70 || num==80 || num==90 || num==100)
     {
      screen[i][j]=rand()%127+1;
     }
     else
     {
      screen[i][j]=' ';    
     }       
     }        
    }
    for(int i=0; i<width-1; i++)
    {
     for(int j=0; j<height-1; j++)
     {
      cout << screen[i][j];       
     }
     Sleep(10);
     cout << endl;      
    } 
    system("PAUSE");
    return EXIT_SUCCESS;
}

Any help would be greatly appreciated
Noticed a number of things, ill go down the list:

[] Line 19, you're if statement. The first statement is the same as saying rand has a 1/10 chance, which is the same is just saying if(num<=10).

[]You never seed your random number generator, which means it will always return the same value unless to seed the computers clock into the function to use. This would be accomplished with srand(time(NULL)). This needs to be called only once through the life of the program, so i would recommend placing it at the top.

[]you declare a two dimensional array of char's yet attempt to read an int into them, especially when the int could be more than one digit. Change screen to int or cast the rand as a char.

[]Your two dimensional array is backwards, you actually have an array of 50 height by 20 width. This can trick a lot of people.

[]Using system("PAUSE"); is a really bad habit, try to break this habit as soon as you can. Refer to the first post in the beginner section on suggestions to replace this code.

[]Unconventional, but usually return EXIT_SUCCESS; is used in error checking for GUI's. Guess this is ok though(should just return the correct value).

Hope this helps.
HAHA YES it does thank you so much idk why i forgot srand(time(0)); but i did.

anyway it keeps making my computer beep for some reason and its really annoying is there anyway to fix this
That would be because you did not read in the input correct, did you change your array to ints?
if i change the array to ints it wont execute what i want it to do i input the ascii value/number and it outputs the ascii art
Note that chars can only hold ONE character, so your char's can only hold a value of 0-9(dont make rand go over 9 then!). Here is a snippet to code where i convert and int to a char variable:

1
2
3
4
5
int num = 5;
    char c = '0';
    for(int i = 0; i < num; i++)
        c++;
    cout << c << endl;
But it displays characters with a value higher than 10
Need4Sleep (509) wrote:

Note that chars can only hold ONE character, so your char's can only hold a value of 0-9(dont make rand go over 9 then!).


A char is just a small int, one just has to be careful the char referred to by the int, is the one that you you think it is, and that the int isn't bigger than the limit for the char (either 127 or 255). I think this is the point for line 21 in the OP's code.

So one can do either of these equivalent:

1
2
char MySpace = 48; //space character in decimal
MySpace = '  ';  //space character as an actual char 


The problem you have at the moment, is if the variables height and width become larger than a char, then there will be problem. The other thing is, you assign the first 50 ascii code to the chars in the array, which includes some things you might not expect - including the bell escape char.

http://www.ascii-code.com/


Variables height and width should be defined as const to avoid the magic number thing.

You could make the variables height and width chars, but this would limit the size of your array.

Need4Sleep is right, the chars need to be converted to int, so you can avoid the limiting problem.

If you just want to put a series of chars into the array, you can use a for loop like this:

1
2
3
4
5
6
char MyCharArray[26];
char MyChar;


for (MyChar = 'a';MyChar <='z'; MyChar++)
    MyCharArray[MyChar -97] = MyChar;  //ascii 97 is 'a' 


Finally, try to avoid constructs like this, because you are explicitly specifying each case:

if(num==10 || num==20 || num==30 || num==40 || num==50 || num==60 || num==70 || num==80 || num==90 || num==100)

Better to do this:

1
2
if (num % 10 == 0 && num <= 100 && num > 0)


This is scalable and more elegant I think.

Hope all goes well.
Thank you TheIdeasMan that does help, but what is a bells char
but what is a bells char


If you look a the ascii table- from the website above, the first part consists of different control characters, number 7 is the one that rings the computer's bell, or rather the Operating System causes the computer to sound a tone.
lol awesome thanks man im gonna use that to mess with my friends in computer programming
Topic archived. No new replies allowed.