Program to assign a value (say 5) to 1st column of 4 by 3 array. 4 = rows & 3 = columns.
#include <iostream>
using namespace std;
int main()
{
int Rehan[4][3]; // This is the way to declare 2-d array named Rehan.
for (int i = 0; i <=3; i++) //This loop performs the following assigning of values.
Rehan[i][0] = 5;
//Rehan[0][0] = 5;
//Rehan[1][0] = 5;
//Rehan[2][0] = 5;
//Rehan[3][0] = 5;
for ( i = 0; i<=3; i++)
cout<<Rehan[i][0]<<endl;
//if we don't use loop then we had to do following.
//cout<<Rehan[0][0]<<endl;
//cout<<Rehan[1][0]<<endl;
//cout<<Rehan[2][0]<<endl;
//cout<<Rehan[3][0]<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int Rehan[4][3]; // This is the way to declare 2-d array named Rehan.
for (int i = 0; i <=3; i++) //This loop performs the following assigning of values.
Rehan[i][0] = 5;
//Rehan[0][0] = 5;
//Rehan[1][0] = 5;
//Rehan[2][0] = 5;
//Rehan[3][0] = 5;
for ( i = 0; i<=3; i++)
cout<<Rehan[i][0]<<endl;
//if we don't use loop then we had to do following.
//cout<<Rehan[0][0]<<endl;
//cout<<Rehan[1][0]<<endl;
//cout<<Rehan[2][0]<<endl;
//cout<<Rehan[3][0]<<endl;
return 0;
}
Now lets make a program for the same number of rows and columns. Storing 5 in the second column of array and then printing the whole array. Sounds Interesting, it's.
#include <iostream>
using namespace std;
int main()
{
int Rehan[4][3]={0};
for (int i = 0; i <=3; i++)
Rehan[i][1] = 5;
//What happens within the loop:
//Rehan[0][1] = 5;
//Rehan[1][1] = 5;
//Rehan[2][1] = 5;
//Rehan[3][1] = 5;
//Now we want to print whole of the array.
for (int rows = 0; rows<=3; rows++)
{
for (int columns = 0; columns <= 2; columns++)
cout<<Rehan[rows][columns]<<" ";
cout<<endl;
}
return 0;
}
Posted by Muhammad Rehan Qadri
No comments:
Post a Comment