C++ Arrays
C++ Arrays
1
1) Array declaration and initialization
1 #include<iostream>
2 using namespace std;
3
4 int main(void)
5 { //array declaration
6 int num1[5];
7
8 //another declaration method
9 const int SIZE=3;
10 int num2[SIZE];
11
12 //array declaration with initialization
13 int num3[5]={12,2,54,7,100};
14
15 //declaration with incomplete initialization
16 int num4[5]={12,2};
17
18 //displaying array contents
19 cout<<num1[0]<<" "<<num1[1]<<" "<<num1[2]
20 <<" "<<num1[3]<<" "<<num1[4]<<endl;
21 cout<<num2[0]<<" "<<num2[1]<<" "<<num2[2]
22 <<endl;
23 cout<<num3[0]<<" "<<num3[1]<<" "<<num3[2]
24 <<" "<<num3[3]<<" "<<num3[4]<<endl;
25 cout<<num4[0]<<" "<<num4[1]<<" "<<num4[2]
26 <<" "<<num4[3]<<" "<<num4[4]<<endl;
27 return 0;
28 }
2
1 #//array declaration and initialization
2 #include<iostream>
3 using namespace std;
4 int main(void)
5 { //array declaration
6 char name[5];
7
8 //another declaration method
9 const int SIZE=3;
10 char pet[SIZE];
11 //array declaration with initialization
12 char vowels[5]={'a','e','i','o','u'};
13 //declaration with incomplete initialization
14 char consonants[21]={'b','c'};
15 //displaying array contents
16 cout<<name[0]<<" "<<name[1]<<" "<<name[2]
17 <<" "<<name[3]<<" "<<name[4]<<endl;
18 cout<<pet[0]<<" "<<pet[1]<<" "<<pet[2]
19 <<endl;
20 cout<<vowels[0]<<" "<<vowels[1]<<" "<<vowels[2]
21 <<" "<<vowels[3]<<" "<<vowels[4]<<endl;
22 cout<<consonants[0]<<" "<<consonants[1]<<" "<<
23 " "<<consonants[3]<<" "<<consonants[4]<<endl;
24 return 0;
25 }
3
3) Array initialization, input and processing
1 #include<iostream>
2 #include<conio2.h>
3 using namespace std;
4 #define SIZE 6
5
6 int main(void)
7 {
8 // Declaring arrays
9 int subjectscores[SIZE];
10 float weeklytemperature[7];
11 char name[25];
12 // similarly long and double can be defined
13 // NOTE: strings are not arrays
14 // Initializing arrays
15 int monthlyprofits[12]=
16 {1200000,6434564,5564664,5456456,5467787,9788445,4456
17 ,39996534,8745647,3142355,6574347,89676};
18 float subjectpercentages[6]={88.5,85.2,85.0,79.5,81.0,75.1};
19 char vowels[5]={'a','e','i','o','u'};
20 // use curly brackets containing values separated by comma
21
22 // taking input in an array from location 0 to 5 (total 6 elements)
23 cout<<"\nEnter element no.0 :";
24 cin>>subjectscores[0]; // scanning is just like any variable
25 cout<<"\nEnter element no.1 :"; // but using the appropriate index no.
26 cin>>subjectscores[1];
27 cout<<"\nEnter element no.2 :";
28 cin>>subjectscores[2];
29 cout<<"\nEnter element no.3 :";
30 cin>>subjectscores[3];
31 cout<<"\nEnter element no.4 :";
32 cin>>subjectscores[4];
33 cout<<"\nEnter element no.5 :";
34 cin>>subjectscores[5];
35 4
36 // Some manipulation/processing with array:
37 // adding 2 to each element
38 subjectscores[0]=subjectscores[0]+2;
39 subjectscores[1]=subjectscores[1]+2;
40 subjectscores[2]=subjectscores[2]+2;
41 subjectscores[3]=subjectscores[3]+2;
42 subjectscores[4]=subjectscores[4]+2;
43 subjectscores[5]=subjectscores[5]+2;
44
45 // printing the processed array
46 cout<<"\nElement 0 = "<<subjectscores[0];
47 cout<<"\nElement 1 = "<<subjectscores[1];
48 cout<<"\nElement 2 = "<<subjectscores[2];
49 cout<<"\nElement 3 = "<<subjectscores[3];
50 cout<<"\nElement 4 = "<<subjectscores[4];
51 cout<<"\nElement 5 = "<<subjectscores[5];
52 getch();
53 return 0;
54 }
5
4) Processing arrays with for() loop
1 #include<iostream>
2 #include<conio2.h>
3 #include<ctime>
4 #include<cstdlib>
5 using namespace std;
6
7 #define LIM 10
8
9 int main(void)
10 {
11 // variable definition
12 // 10 element float array, numbered 0 to 9
13 float num[LIM]={12.0,3.3,7.6,3.1,9.8,7.9,3.4,8.7,9.0,1.0};
14 int index_count;// an integer variable to access different array locations
15 srand(time(NULL));
16 //Automatic initialization with even numbers
17 for(index_count=0;index_count<LIM;index_count++)
18 {
19 num[index_count]=(index_count+1)*2;
20 } 6
21
22 cout<<"\nPress any key to continue";
23 getch();
24
25 // loop for printing output
26 for(index_count=0;index_count<LIM;index_count++)
27 {
28 cout<<"\nElement no."<<index_count<<" = "<<num[index_count];
29 }
30 cout<<endl<<endl;
31
32 //Automatic initialization with random numbers
33 for(index_count=0;index_count<LIM;index_count++)
34 {
35 num[index_count]=(rand()%100)+1;
36 }
37
38 cout<<"\nPress any key to continue";
39 getch();
40
41 // loop for printing output
42 for(index_count=0;index_count<LIM;index_count++)
43 {
44 cout<<"\nElement no."<<index_count<<" = "<<num[index_count];
45 }
46 cout<<endl<<endl;
47
48 // taking array input through for() loop: index_count runs from 0 to 9
49 for(index_count=0;index_count<LIM;index_count++)
50 {
51 cout<<"\nEnter element number "<<index_count<<":";
52 cin>>num[index_count];
53 }
54
55
56
57 7
58
59 // processing the array: adding 2 to each element
60 for(index_count=0;index_count<LIM;index_count++)
61 {
62 num[index_count]=num[index_count]+2.0;
63 }
64 cout<<"\nTwo (2.0) shall be added to each location now......";
65 cout<<"\nPress any key to continue";
66 getch();
67 // loop for printing output
68 for(index_count=0;index_count<LIM;index_count++)
69 {
70 cout<<"\nElement no."<<index_count<<" = "<<num[index_count];
71 }
72
73 getch();
74 return 0;
75 }
1 #include<iostream>
2 #include<conio2.h>
3 using namespace std;
4
5 #define LIM 10
6
7 int main(void)
8 {
9 // variable definition
10 float num[LIM]; //10 element float array, numbered 0 to 9
11 float sum=0.0,average;//
12 int index_count; //an integer variable to access different array locations
13
14 // taking array input through for() loop: index_count runs from 0 to 9
15
8
16 for(index_count=0;index_count<LIM;index_count++)
17 {
18 cout<<"\nEnter element number "<<index_count<<":";
19 cin>>num[index_count];
20 }
21
22 cout<<"\nAverage is being calculated now......";
23 cout<<"\nPress any key to continue";
24 getch();
25 // processing the array: calculating running sum
26 for(index_count=0;index_count<LIM;index_count++)
27 {
28 sum=sum+num[index_count];
29 }
30 average=sum/LIM;
31 // printing output
32 cout<<"\nAverage of given 10 nos. ="<<average;
33 getch();
34 return 0;
35 }
10
7) Array with while() loop
1 #include<iostream>
2 #include<conio2.h>
3 using namespace std;
4
5 int main(void)
6 {
7 const int MAX=100;
8 // Array/variable definition
9 char name[MAX]; // char array of 100 elements
10 int index; // integer variable to address array locations
11 int maxindex; // integer variable to store number of elements utilised
12
13 cout<<"\nEnter your name (press ESC to stop)\n";
14
15 // Taking input in array locations: index goes from 0 to unknown value
16 index=0; // setting index to 0 before loop
17 name[index]=getche();// Initialising first element from user
18 // before running for the first time
19
20 while( name[index]!=27 ) // loop ends when ESC is pressed
21 {
22 index++;
23 if(index==100) //
24 { // Extremely Important 1:
25 cout<<"\nArray Overflow\n"; // Provide breaking mechanism
26 break; // in case of overflow
27 } //
28 name[index]=getche();
29 }
30
31 maxindex=index; //Extremely Important 2: saving the the last element
32 //number given by user
33 // Simple processing of name[] array, converting small into capital case
34 // index goes upto maxindex less 1
35 11
36
37 for(index=0; index<maxindex; index++)
38 {
39 if((name[index]>=97)&&(name[index]<=122))
40 {
41 name[index]=name[index]-32;
42 }
43 }
44 cout<<"\nPrinting the processed array.\n";
45 // Printing array elements to see the change made
46 for(index=0;index<maxindex; index++)
47 {
48 cout<<name[index]; // displaying each location
49 }
50 getch();
51 return 0;
52 }
21
15) Passing arrays to functions
1 //Passing arrays to functions
2 #include<iostream>
3 #include<conio2.h>
4 using namespace std;
5 #define MAX 100
6
7 //Prototype 1:function to display a double array
8 void displaydoublearray(const double arr[],int start,int stop, int SIZE);
9
10 //Prototype 2:function to find and return maximum value of double array
11 double maxofdoublearray(const double arr[],int start,int stop);
12
13 //Prototype 3:Input double array from user
14 void inputdoublearray(double arr[],int start,int stop, int SIZE);
15
16 int main (void)
17 {
18 // Variable definition and declaration
19 double num[MAX];
20 double largest;
21 int elements;
22 cout<<"How many array elements do you need:";
23 cin>>elements;
24 //input array elements from user
25 inputdoublearray(num,0,elements-1,MAX);
26 //display array
27 cout<<"\nThe array you entered is:\n";
28 displaydoublearray(num,0,elements-1,MAX);
29 //find largest element of the given array
30 largest=maxofdoublearray(num,0,elements);
31 cout<<"\nThe largest among the given numbers"
32 <<"is "<<largest;
33 return 0;
34 }
35 22
36 //***************** Definition of displaydoublearray()**********************
37 //Definition 1:
38 void displaydoublearray(const double arr[],int start,int stop,int SIZE)
39 {
40 int index;
41 //Displaying the array
42 if((SIZE>stop)&&(start>=0))
43 {
44 for(index=start;index<=stop;index++)
45 {
46 cout<<arr[index]<<" ";
47 if((index+1)%5==0)
48 {
49 cout<<endl;
50 }
51 }
52 }
53 else
54 {
55 cout<<"\n\nERROR:Array limits exceeded.";
56 }
57 }
58 //*****************Definition of maxofdoublearray()********************
59 //Definition 2:function to find and return maximum value of double array
60 double maxofdoublearray(const double arr[],int start,int stop)
61 {
62 int index;
63 float maximum=arr[start];//assuming the first element to be maximum
64 //Defensive condition: program runs only if this condition is met
65 //Loop to go through all elements start_index---->stopindex
66 for(index=start;index<=stop;index++)
67 {
68 if(arr[index]>maximum)//if current assumption is wrong
69 {
70 maximum=arr[index];// select a new max
71 }
23
72 }
73 return maximum;
74 }
75 //*****************Definition of inputdoublearray()**************************
76 //Definition 3:Input double array from user
77 void inputdoublearray(double arr[],int start,int stop,int SIZE)
78 {
79 int index;
80 if((SIZE>stop)&&(start>=0))
81 {
82 for(index=start;index<=stop;index++)
83 {
84 cout<<"Enter location "<<index<<":";
85 cin>>arr[index];
86 }
87 }
88 else
89 {
90 cout<<"\n\nERROR:Array limits exceeded.";
91 }
92 }
32
20) 2D Array Application: Computing student average and score average
1 #include <iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 const int rows = 5;
8 const int cols = 5;
9 int total = 0;
10 double average = 0.0;
11 //each row contains scores of 5 test of a student
12 //each column contains score in a single test taken
13 //by all 5 students
14 int grades[rows][cols] = {{75, 82, 84, 79, 91},
15 {85, 81, 94, 96, 89},
16 {92, 91, 94, 89, 90},
17 {74, 72, 81, 78, 80},
18 {84, 82, 82, 83, 81}};
19 //printing student average for all 5 tests
20 for(int r = 0; r < rows; ++r)
21 {
22 cout << "Student " << r+1 << ": ";
23 for(int c = 0; c < cols; ++c)
24 {
25 cout << grades[r][c] << " ";
26 total += grades[r][c];
27 }
28 average = total / cols;
29 cout << "Average: " << average << endl;
30 total = 0;
31 average = 0.0;
32 }
33 cout << endl;
34 //printing test average for all 5 stuents
35 33
36 for(int c = 0; c < cols; ++c)
37 {
38 cout << "Test " << c+1 << ": ";
39 for(int r = 0; r < rows; ++r)
40 {
41 cout << grades[r][c] << " ";
42 total += grades[r][c];
43 }
44 average = total / rows;
45 cout << "Average: " << average << endl;
46 total = 0;
47 average = 0.0;
48 }
49 return 0;
50 }
37