C++ help!

hello guys! i am new in here...I have a C++ homework assignment that wants me to create three functions.
I just wrote the first two functions and I need your help to see if there's something wrong with the code. I think it's wrong because when I compile it, it gives me one column! What do you think?
PS: I removed my code(sorry)
Last edited on
You forgot to include the headers ctime and cstdlib.

1
2
#include <ctime>
#include <cstdlib> 


Even if your C++ compiler doesn't complain about missing definitions for std::time() and std::srand(), it doesn't mean your code is correct. Always include the headers from which you're using something.

Next, the function declarations ("prototypes") are incorrect. Basically your definitions and declarations don't match.

1
2
3
4
const int N=5;

void FillArray(int[N][N]);
void PrintArray(int[N][N]);


Next, std::srand() seeds the random number generator. This function should only be called once in the program. Most people call it once in main(). So move it from inside the for() loops into main():

1
2
3
4
5
6
7
8
int main()
{
    int arr[N][N];

    srand(time(0));
    FillArray(arr);
    PrintArray(arr);
}


In your PrintArray() function you forgot to put a newline after each line is printed, and setw(3) should be put in front of arr[c][r]. You also need to practice your indent style.

1
2
3
4
5
6
7
8
9
10
11
12
13
void PrintArray(int arr[N][N])
{
    for(int r=0;r<N;r++)
    {
        for(int c=0 ; c<N ;c++)
        {
            // arr[c][r]=(rand()%998) + 44; // print only, don't modify!
            cout << setw(6) << arr[c][r];
        }

        cout << '\n';
    }
}


Edit: and I just noticed, why are you modifying the arr array in the PrintArray() function?
Last edited on
omg thank you so much :D and i only have the last function if i have anything i will ask you because you are a genius!! thank you!
and just one question what do you mean by "why are you modifying the arr array in the PrintArray() function?"
what do you mean by "why are you modifying the arr array in the PrintArray() function?"

The PrintArray() function should print the array arr. It should not modify it.

If you look at the code in my previous post, the assignment of random values was commented out:

// arr[c][r]=(rand()%998) + 44; // print only, don't modify!

The FillArray() function should do the opposite: it should fill the array arr with random values, but should not print each element.

The FillArray() function is left for you to correct but it's easy: just remove the printing, and remove the call to std::srand().
oh okay you are so right! thank you again




you're the best
Last edited on
Yes, looks good to me. As for indent style, keep practicing. Be consistent in your formatting.

Here's my style (the code is the same as yours).
Personally, I prefer to use tabs of length 4 and inserting them as spaces.
See the settings in your editor (hopefully it's not Notepad).

1
2
3
4
5
6
7
8
9
10
void FillArray(int arr[N][N])
{
    for(int r=0; r < N; r++)
    {
        for(int c=0; c < N; c++)
        { 
            arr[c][r] = (rand() % 998) + 44;
        }
    }
}

okay thank you
i have a question if i want those random number to be between 45 and 998
i think (rand() % 998) + 44 is wrong...
do you have any method to teach me how to make it right?
Let's say you want a number from 0 to 9.
You write: rand() % 10

Now let's say you want a number from 3 to 9.
If you write (rand() % 10) + 3 that's wrong, because now it goes from 3 to 12:

0 1 2 3 4 5 6  7  8  9 +
3 3 3 3 3 3 3  3  3  3 =
-----------------------------
3 4 5 6 7 8 9 10 11 12


So how high do we need to go to reach the value 9?
Only as high as 6, which means (rand() % 7).
So the result is: (rand() % 7) + 3

Long story short, the formula is:

(rand() % (max+1 - min)) + min

where max is the highest value you wish to get, and min is the lowest.

Note that this all blows up if (min > max) or if they're negative values.
thank you so much so easy!
i am sorry that i bothered you... Our teacher didn't teach us any of that i am trying to figured it out myself!
you are better than my teacher :)
catfish666 can you help me with the last part?
because i tried different methods and none of them worked :( and i just had enough :/
we want to create a function that organize the array like this(diagonally):

for example:

1
2
3
4
3    6   1   2
7    5   9   8
13   15  11  10
14   17  19  16


becomes:

1
2
3
4
1    2   5   8 
3    6   9   13
7   10   14  16
11  15   17  19


any ideas how?
catfish666 can you help me with the last part?

If you keep asking me specifically for help, what you do is alienate everybody else. This means they won't bother to help you, which is detrimental to you.

Now back to the subject, I have a small observation: in your code (which you deleted) I noticed that you mixed up the columns and lines.

Your code works fine though, because you used the same N for both width and height. Otherwise nasty things may have happened.

Here is the "traditional" way to go through a 2D array (notice which for() loop iterates what):

1
2
3
4
5
int arr[3][10];

for (int i=0; i < 3; ++i)
    for (int j=0; j < 10; ++j)
        arr[i][j] = 0;


So you have a 2D array and you need to do a weird diagonal sorting kind of thing.

I'd suggest simplifying things: copy all the contents of the array into a vector, sort the vector, then copy the now sorted contents of the vector back into the array.

Copying to a vector and sorting it is the easy bit.

1
2
3
4
5
6
7
8
9
10
11
12
#include <algorithm>
#include <vector>

// ...

std::vector<int> arr_contents;

for (int i=0; i < N; ++i)
    for (int j=0; j < N; ++j)
        arr_contents.push_back(arr[i][j]);

std::sort(arr_contents.begin(), arr_contents.end());


Now we need to copy from arr_contents back to arr in a certain way.

 |0 1 2
-+-----
0|a b d
1|c e g
2|f h i

a[0][0]

b[0][1]
c[1][0]

d[0][2]
e[1][1]
f[2][0]

g[1][2]
h[2][1]

i[2][2]


Do you see some patterns? As in, some kind of shift happening between the coordinates?

a[0][0] no shift

b[0][1] shift: i+1, j-1
c[1][0] stop

d[0][2] shift: i+1, j-1
e[1][1] shift: i+1, j-1
f[2][0] stop

g[1][2] shift: i+1, j-1
h[2][1] stop

i[2][2] no shift


Figure out why the shifting stops (should be obvious).

Then figure out how you move to the next starting element from where the shifting begins. Starting elements are: a, b, d, g, i.

Hint: check if in current starting element arr[i][j], j equals N-1.
the teacher didn't teach us the algorithm or the vector thing that's why it's harder for me to solve this :/ anyway the only thing he taught us is: the bubble sort method and the swaping... can we sort them with these two methods?
P.S: i am a beginner in C++
Well if you don't want to use std::vector and std::sort() then you can use a regular array (instead of the vector) and your own bubble sort function (instead of C++ standard sort).

1
2
3
4
5
6
7
8
9
10
11
int arr_contents[N * N];
int ac_index=0;

for (int i=0; i < N; ++i)
    for (int j=0; j < N; ++j)
    {
        arr_contents[ac_index] = arr[i][j];
        ++ac_index;
    }

bubble_sort(arr_contents, N * N);


Everything else stays the same.
Last edited on
so where should i put this?:

[code]int arr_contents[N * N];
int ac_index=0;


for (int i=0; i < N; ++i)
for (int j=0; j < N; ++j)
{
arr_contents[ac_index] = arr[i][j];
++ac_index;
}

bubble_sort(arr_contents, N*N);[/code
Last edited on
I told you about formatting, it seems you ignored my advice on that, completely.

1
2
3
4
5
6
7
8
9
10
11
void PrintTheArray(int arr[N][N])
{
	for(int r=0 ; r<N ;r++)
     {
		 for(int c=0 ; c<N ;c++)
		 {
 cout<<setw(6)<< arr[c][r];
}
cout<<'\n';
}
}


okay so this is my coding:

Your code is not standard-compliant C++ code. You cannot define functions in the body of another function.

in my third function?

I guess it would be the fourth function, if counting main(). Could be something named SortTheArray().

1
2
3
4
5
6
void SortTheArray(int arr[N][N])
{
    // 1) copy from arr to arr_content
    // 2) sort arr_content, either right in here, or by using another function
    // 3) copy from arr_content to arr as discussed above
}

i didn't ignore your advice but it was not the complete coding ... i will fix all the things when i finish it....
anw i didn't get the fourth function but here what i did(sorry for the late reply):
1
2
3
4
5
6
7
8
9
10
void SortTheArray(int arr[N][N])
{int arr_contents[N * N];
    int ac_index=0;
for (int i=0; i < N; ++i)
    for (int j=0; j < N; ++j)
    {
        arr_contents[ac_index] = arr[i][j];
        ++ac_index;
    };
}
Last edited on
Topic archived. No new replies allowed.