[go: up one dir, main page]

0% found this document useful (0 votes)
120 views9 pages

C++ Array Practice for Beginners

Kasus 1-4 demonstrate the use of arrays in C++ programs, including initializing arrays, accessing array elements, and nested for loops to iterate through multi-dimensional arrays. Kasus 5-7 show how to declare and initialize single and multi-dimensional arrays, as well as output the values stored in the arrays. Kasus 8-13 provide additional examples of using arrays in C++, such as storing user input, calculating averages, and initializing arrays within for loops to generate values.

Uploaded by

HAPPY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views9 pages

C++ Array Practice for Beginners

Kasus 1-4 demonstrate the use of arrays in C++ programs, including initializing arrays, accessing array elements, and nested for loops to iterate through multi-dimensional arrays. Kasus 5-7 show how to declare and initialize single and multi-dimensional arrays, as well as output the values stored in the arrays. Kasus 8-13 provide additional examples of using arrays in C++, such as storing user input, calculating averages, and initializing arrays within for loops to generate values.

Uploaded by

HAPPY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Kasus 1

#include <iostream>
using namespace std;

int foo[] = {16, 2, 77, 40, 12071};


int n, result=0;

int main ()
{
for (n=0; n<5; ++n)
{
result += foo[n];
}
cout << result;
return 0;
}

Kasus 2
#include <iostream>
using namespace std;

void printarray (int arg[], int length) {


for (int n=0 ; n<length; ++n)
cout<<arg[n]<<' ';
cout<< '\n';
}

int main()
{
int firstarray[]={5, 10, 15};
int secondarray[]={2, 4, 6, 8, 10};
printarray (firstarray, 3);
printarray (secondarray,5);

Kasus 3
#include <iostream>
using namespace std;

int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 Numbers : ";
//storing 5 number entered by user in array
//finding the sum of number entered
for (int i = 0; i<5; ++i)
{
cin>>numbers[i];
sum+=numbers[i];
}
cout<<" SUM = "<<sum<<endl;

return 0;
}

Kasus 4
#include <iostream>
using namespace std;

int main()
{
int x;
int y;
int array[8][8]; // declares an array like a chessboard

for(x=0; x<8; x++) {


for (y=0; y<8; y++)
array[x][y]=x*y; // set each elament to a value
}
cout<<"Array Indices : \n";
for (x=0; x<8; x++) {
for(y=0; y<8; y++)
cout<<"["<<x<<"]["<<y<<"]"<<array [x][y]<<" ";
cout<<"\n";
}
cin.get();
}

Kasus 5
#include <iostream>
int main()
{
short age[4];
age[0]=23;
age[1]=34;
age[2]=65;
age[3]=74;
std::cout<<age[0]<<std::endl;
std::cout<<age[1]<<std::endl;
std::cout<<age[2]<<std::endl;
std::cout<<age[3]<<std::endl;

return 0;
}

Kasus 6
#include <iostream>

int main()
{
short age[4];
short same_age[4];

age[0] = 23;
age[1] = 34;
age[2] = 65;
age[3] = 74;

same_age[0] = age[0];
same_age[1] = age[1];
same_age[2] = age[2];
same_age[3] = age[3];

std::cout<<same_age[0]<<std::endl;
std::cout<<same_age[1]<<std::endl;
std::cout<<same_age[2]<<std::endl;
std::cout<<same_age[3]<<std::endl;
return 0;
}

Kasus 7
#include <iostream>
using namespace std;

int main()
{
int test[3][2] =
{
{2, -5},
{4, 0},
{9, 1}
};

for (int i = 0; i<3; ++i)


{
for(int j = 0; j<2; ++j)
{
cout << "test ["<<i<<"] ["<<j<<"]"<<test[i][j]<<endl;
}
}
return 0;
}

Kasus 8
#include <iostream>
using namespace std;

const int CITY=2;


const int WEEK=7;

int main()
{
//inserting the values into the temperature array
int temperature [CITY][WEEK];
cout<< "Enter all temperature for a week of first city and then second city. \n";

for (int i=0; i < CITY; ++i)


{
for (int j = 0; j<WEEK; ++j)
{
cout<<"City "<<i+1<<", Day"<<j+1<<" : ";
cin>>temperature[i][j];
}
}
cout<<"\n \n Displaying Values : \n";
//accessing the values from the temperature array

for(int i = 0; i<CITY; ++i)


{
for(int j = 0; j<WEEK; ++j)
{
cout<<"City"<<i+1<<", Day"<<j+1<<" = "<<temperature[i][j]<<endl;
}
}
return 0;
}

Kasus 9
#include <iostream>
using namespace std;

int main()
{
//this array can store up to 12 elements (2x3x2)
int test[2][3][2];
cout<<"Enter 12 Values: \n";

//inserting the values into the test array


//using 3 nested fo loops.

for(int i=0; i<2;++i)


{
for(int j=0; j<3;++j)
{
for(int k=0; k<2;++k)
{
cin>>test[i][j][k];
}
}
}

//displaying the values with proper index.


for(int i=0; i<2;++i)
{
for(int j=0; j<3;++j)
{
for(int k=0; k<2;++k)
{
cout<<"test ["<<i<<"]["<<j<<"]["<<k<<"] = "<<test[i][j][k]<<endl;
}
}

return 0;
}

Kasus 10
#include <iostream>
using namespace std;

int main()
{
//deklarasi struct dengan nama mahasiswa
struct mahasiswa
{
//isi dari tipe data bentukan mahasiswa
int nim;
char nama[20];
char gender[10];
};

//inisialisasi mhs ke tipe data mahasiswa dengan arraynya


mahasiswa mhs[2];

//perulangan untuk menginputkan


for (int i = 0; i<2; i++)
{
cout <<"NIM : ";
cin>>mhs[i].nim;

cout <<"Nama : ";


cin>>mhs[i].nama;

cout <<"Jenis Kelamin : ";


cin>>mhs[i].gender;
}

//perulangan untuk menampilkan


for (int i=0; i<2; i++)
{
cout<<"========================== \n";
cout<<"NIM : "<<mhs[i].nim<<endl;
cout<<"Nama : "<<mhs[i].nama<<endl;
cout<<"Jenis Kelamin : "<<mhs[i].gender<<endl;
}

return 0;
}

Kasus 11
#include <iostream>
using namespace std;

int main()
{
int nilai[10];

for (int p=0; p<10; p++)


{
nilai[p] = p+2;
};

for (int p=0; p<10; p++)


{
if (p%4==0)
{
cout<<" ";
}
else
{
cout<<nilai[p];
};
}
}

Kasus 12
#include <iostream>
using namespace std;
int main()
{
int nilai[5], x, jumlah, rata;

for(x=0; x<5; x++)


{
cout << "Masukkan Nilai Mahasiswa "<<x<< " : ";
cin>>nilai[x];
}

for(x=0; x<5; x++)


{
jumlah= jumlah + nilai[x];
}
rata= jumlah /5;

cout<<"Nilai Rata-rata : "<<rata;


}

Kasus 13
#include <iostream>
using namespace std;

int main()
{
int i, j, x, n;
int array[x];

cin>>n;
for (i=0 ; i<=n; i++ )
{
for (j=0; j<=n; j++ )
{
x=j;
if (x<=i)
{
x=j+1;
array[x];
}
else
{
x=0;
array[x];
};
}
}

for (i=1 ; i<=n; i++ )


{
for (j=1; j<=n; j++ )
{
cout<<array[x]<<"\n";
};
}

You might also like