Hello.
I have a question regarding a task I was given.
It goes : Fill the array with random numbers and then print out how many times theyve appeared like so :

``````/*
ARRAY : 0 0 0 0 1 1 1 2 2 2 2 2 6 6 6 6 6 6 4 4 5 5 5
OUTPUT :
^
^   |
^       |   |
|   ^   |   |       ^
|   |   |   |   ^   |
|   |   |   |   |   |
---------------------
4   3   5   6   2   3
0   1   2   3   4   5
*/``````

So Ive decided to use a char 2D array for the graphical part, and one additional 1D array where Ive stored the number of occurences of each element. The problem is that I dont know how to format that 2D array so that it could resemble that picture. My 2D array looks like this

``````/*
ARRAY : 0 0 0 0 1 1 1 2 2 2 2 2 6 6 6 6 6 6 4 4 5 5 5
OUTPUT :
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
|   |   |   |   |   |
---------------------
4   3   5   6   2   3
0   1   2   3   4   5
*/``````

I have found a max number in the occurences array which in this example is six, so there are six rows. I would be grateful if someone could help me with the logic of this problem, or how could I place those damn little '^' caps where they should be. Thank you very much for any given aid!!

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by Joey_Brown

You can do this with just one pair of nested loops, but I like using two to simplify it.

The first pair of nested loops fills the char array a[][] with the correct values. The second pair of nested loops, prints them out.

Clear out the array, to just spaces:

``````char a[R][C];

//zero it out:
for(r=0;r<R;r++)
for(c=0;c<C;c++)
a[r][c] = ' ';  //a space``````

Now the real loops in pseudo code:

``````for each col
for each row  //like turning the array 90 degrees counter clockwise
if row < value in your occurrences array for col's value
a[row][col] = '|'
else if row == value in your occurrences arrary
a[row][col = '^'
end of for each row
end of for each col

Now you're ready to print, but it has to be from the top down, so it will display with the higher values being higher up on the screen.

for r=R; r> -1; r--
for c=0; c < C;c++ //still going upward on c, so it reads left to right
print a[r][c];
end of for c=0...
putchar('\n'); //end of a row
end of for r=R...``````

And there you go. Add your footer lines, and anything you need along the vertical on the far left or right hand side, for the scale.

;)

You can do this with just one pair of nested loops, but I like using two to simplify it.

The first pair of nested loops fills the char array a[][] with the correct values. The second pair of nested loops, prints them out.

Clear out the array, to just spaces:

;)

Hey Adak, thanks!! I will implement and get back., Much thanks m8 :)