🔒 Closed 2d array help po

Status
Not open for further replies.

lemmor

Honorary Poster
Pano po gagawin sa 2dimensional array ang ganitong row,
X y x^2 y^2
1 6 1 36
2 7 4 49
3 8 9 64
4 9 16 81
5 10 25 100

simple array kaya ko pero ganitong setup sa 2dimensional pano kaya pa help naman po sa mga expert..TIA.
 
Code:
//Diba declaring 1d arrays is this?
int newArray[] = newArray[4];
newArray[0] = 1;
newArray[1] = 2;
newArray[2] = 3;
//And so on

//Output
// 1
// 2
// 3

//Ito naman for multiD
int[][] newArray = new int[4][3];
//Declaration for 5 rows and 4 column
//Kita ang difference and similarities?

newArray[0][0] = 1;
newArray[0][1] = 6;
newArray[1][0] = 2;
//...and so on

int newArray[][] = {{1,6,1,36},{2,7,4,49},{3,8,9,64},{4,9,16,81},{5,10,25,100}};
//Or declare na diretso with values
//Dito yung first {} inside is [0] then values inside it is [0][0] = 1 and so on
//Yung first [] ay row number
//Second is column
 
I have done this po pero ang output na lumalabas is this
x
1
2
3
4
5
6
7
8
9
10 using the for nestedloop pwede ko po bang makita ang code nyo po kung papano ginawa.
 
int [][] twoDim = new int [5][5];

int a = (twoDim.length);//5
int b = (twoDim[0].length);//5

for(int i = 0; i < a; i++){ // 1 2 3 4 5
for(int j = 0; j <b; j++) { // 1 2 3 4 5
int x = (i+1)*(j+1);
twoDim[j] = x;
if (x<10) {
System.out.print(" " + x + " ");
} else {
System.out.print(x + " ");
}
}//end of for J
System.out.println();
}//end of for i


Ayan po yung code para multiD para sa gusto mong row ikaw nalang po ang bahala doon sa logic ng (x^2) at (y^2)
 
I have done this po pero ang output na lumalabas is this
x
1
2
3
4
5
6
7
8
9
10 using the for nestedloop pwede ko po bang makita ang code nyo po kung papano ginawa.

Code:
for (i = 0; i < 5; i++) {                   //for rows
    for (j = 0; j < 4; j++) {
        System.out.print(newArray[i][j] + " ");
    }
    System.out.println();
}

Use nested for loops po like that
E: Ah I forgot how to use length sa 2d arrays :) incorporate mo nalang dyan sabi ni sir sobciana gawin newArray.length then newArray[0].length
 
Its on c++ palang po kami. Using the nested loop iyon po talaga ang lumalabas sa console ang
X
1
2
3
4
5
6
7
8
9
10
 
Here is my code.
int i,j;
int myarray[2][5]= {{12345},{678910}};
for(i=0;i<2;++i){
for(j=0; j<5;++j){
cout << myarray[j]<<endl;
Ano po kaya lulang dito para lumabas iyong output na gusto ko.
 
Here is my code.
int i,j;
int myarray[2][5]= {{12345},{678910}};
for(i=0;i<2;++i){
for(j=0; j<5;++j){
cout << myarray[j]<<endl;
Ano po kaya lulang dito para lumabas iyong output na gusto ko.
May kulang dito sa last
myarray[j] lagyan so myArray[i][j]
 
Code:
int myArray[2][5] = {{1,2,3,4,5},{6,7,8,9,10}};

    for (int i=0; i < myArray.length; i++) {
        for (int j=0; j < myArray[0].length; j++) {
            cout << myArray[i][j] << endl;
        }
    }
 
Nag typo error lang po kaya wala iyong i..pero ang lumalabas po sa output pag nag run na po sya is
X
1
2
3
4
5
6
7
8
9
10
dapat po ang output is
X y x^2 y^2
1 6 1 36
2 7 4 49
3 8 9 64
4 9 16 81
5 10 25 100 ito posana na output ang lalabas.
 
Ito po sir lemmor change nyo nalang to your needs
Code:
int main() {
int myArray[2][5] = {{1,2,3,4,5},{6,7,8,9,10}};

    for(int i = 0; i < 2; i++) {                   //loop on row
        for(int j = 0; j < 5; j++) {              //loop on col
            cout << myArray[i][j]  << "  ";
        }
        cout << endl;
    }
}
 
bakit ganito po na lumabas na output sir epcraft93..
12345
678910
ang hinahanap ko na out ay ganito po sana
1 6
2 7
3 8
4 9
5 10
 
bakit ganito po na lumabas na output sir epcraft93..
12345
678910
ang hinahanap ko na out ay ganito po sana
1 6
2 7
3 8
4 9
5 10
Code:
int main() {
int myArray[2][5] = {{1,2,3,4,5},{6,7,8,9,10}};

    for(int i = 0; i < 5; i++) { //loop for col values
        for(int j = 0; j < 2; j++) { //loop for row values
            cout << myArray[j][i]  << "  "; 
        }
        cout << endl;         //creates new line after row is created
    }
}
Yan. Binaliktad ko lang yung variables
Yung programming kase eh most of the time trial and error talaga yan.. Sa susunod mag explore ka ok? :D
Wala naman mawawala kung mag explore hehe
 
Ma hingi ko lang po sana ang output ang rest ako na po ang bahala.... tumugma lang sana sa hinihingi ko po di lang ito ang output nya mahaba po ito kasi regression pinapa gawa sa amin.. pa help naman po.pls..
 
Ma hingi ko lang po sana ang output ang rest ako na po ang bahala.... tumugma lang sana sa hinihingi ko po di lang ito ang output nya mahaba po ito kasi regression pinapa gawa sa amin.. pa help naman po.pls..

Kelangan mo din po kasi magtrial and error. Hindi po spoon and feed ang programming kasi nabigyan ka na po ng logic ikaw na dapat po ang magexplore non sir
 
Status
Not open for further replies.

Similar threads

About this Thread

  • 27
    Replies
  • 1K
    Views
  • 4
    Participants
Last reply from:
lemmor

Online now

Members online
417
Guests online
894
Total visitors
1,311

Forum statistics

Threads
2,275,079
Posts
28,960,506
Members
1,233,574
Latest member
Toktokpaff
Back
Top