Click here to Skip to main content
15,913,263 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can you please tell me why is it possible to have codes like this?
#include <iostream>

using namespace std;

int main()
{
    int arr[4][4];
    int a, b;
    int temp;
    cout << "Enter four numbers: \n";
    for (a=0; a<4; a++)
    {
        cin >> arr[a][a];  >especially in this case is it alright to have code like this and why? because i was so confuse why is it okay to have a code like that  
    }
    cout << endl;
    cout <<"Even numbers: \n";
    for (a=0;a<4;a++)
    {
        if (arr[a][a]%2==0)
        {
            cout << arr[a][a]<<endl;
        }
    }
    cout << endl;
    cout << "Highest to lowest: "<< endl;
    for (a=0; a<4;a++)
    {
        for (b=a+1;b<4;b++)
        {
            if (arr[a][a]<arr[b][b])
            {
                temp=arr[a][a];  >and also can you please explain how sorting works? 
                arr[a][a]=arr[b][b];
                arr[b][b]=temp;
            }
        }
    }
    for(a=0;a<4;a++)
    {
        if (arr[a][a]%2==0)
        {
            cout<<arr[a][a]<<endl;
        }
    }
    return 0;
}
I'm sorry if I ask too much because I was a beginner in programming :< thank you


What I have tried:

I tried this code but I didn't expect it will run
Posted
Updated 7-Mar-20 1:40am
v2
Comments
Patrice T 7-Mar-20 6:45am    
Where did you get that code from ?
jusjua 7-Mar-20 7:14am    
I just made it by myself because this was a lab activity at my school

It is perfectly valid to use the same value for both dimensions of a bidimensional array. Provided this value is a valid index into both dimensions.
Here, a variable gets successive values 0, 1, 2 and 3; this results in accessing respectively a[0][0], a[1][1], a[2][2] and a[3][3]. Why do you think this should not run?
 
Share this answer
 
Comments
Member 14765979 7-Mar-20 5:02am    
because I didn't expect it will run
phil.o 7-Mar-20 5:08am    
Yes, that is what you said. My question is: why don't you expect it to run? Why do you think this is incorrect?
jusjua 7-Mar-20 5:15am    
ohh, because I'm a beginner and i always think that 2d array is only compatible in this code like this arr[a][b], so when I try the code arr[a][a] I was confused at the same time happy because the program accept my code
phil.o 7-Mar-20 5:42am    
a and b are just variables; logically, arr[a][a] and arr[a][b] are equivalent whenever a == b. Here, using the same value for both dimensions just means that you are accessing the elements in the diagonal of the square matrix represented by the array (see solution 2 for a graphical representation).
The only constraint to a and b is that they have to be a valid index into the array. Specifically, they must be greater than or equal to zero, and lower than the number of elements in relevant dimension.
Member 14765979 7-Mar-20 17:46pm    
so is it okay to code like this in cin>>arr[a][a]?
Just because you allocate a 4 x 4 array (16 elements) that doesn't mean you have to use them - the compiler doesn#t know (or care) that you don't, as long as all the elements you try a=to access are within the 4 x 4 block:
0, 0  0, 1  0, 2  0, 3
1, 0  1, 1  1, 2  1, 3
2, 0  2, 1  2, 2  2, 3
3, 0  3, 1  3, 2  3, 3
And the reason it doesn't care is simple: it has no idea if you have finished your code, or you have written a bit of it, are testing that it works, and will expand the number of elements you use later with additional code when it's OK so far. That's a reasonable way to do things - I do it all the time, write a bit, check it, write some more, check that, repeat.

So yes, your code could be simpler:
C++
int arr[4];
    int a, b;
    int temp;
    cout << "Enter four numbers: \n";
    for (a=0; a<4; a++)
    {
        cin >> arr[a];
    }
    cout << endl;
    cout <<"Even numbers: \n";
    for (a=0;a<4;a++)
    {
        if (arr[a]%2==0)
        {
            cout << arr[a]<<endl;
        }
    }
    cout << endl;
But it's perfectly valid to write more complex code that does the same thing!
 
Share this answer
 
You also asked about sorting. The algorithm you're using is called a bubble sort[^]. The linked article has links to other ones that describe other sorting techniques.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900