> T1[count]; } 10 is wrong in location to the right only 9 because from 0 to 9 X = 10"> > T1[count]; } 10 is wrong in location to the right only 9 because from 0 to 9 X = 10">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.

Similar presentations


Presentation on theme: "Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements."— Presentation transcript:

1 Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements of the same index of T1 and T2 in the first 10 elements of T3 the results of the multiplication of each elements of the same index of T1 and T2 in the last 10 elements of the array T3. Display T3.

2 Function 1 : Enter values T1 & T2
Display prompt to type 10 values Enter T1[0] Enter T1[1] Enter T1[2] ………. Enter T1[10] 3 5 1 4 6 11 2 33 T1 cout << "Please enter " << X << " integer elements of the first array." << endl; for(int count = 0; count < X; ++count) { cout << "array1[" << count << "]: "; cin >> T1[count]; } 10 is wrong in location to the right only 9 because from 0 to 9 X = 10

3 Function 1 : Enter values T1 & T2
Display prompt to type 10 values Enter T2[0] Enter T2[1] Enter T2[2] ………. Enter T2[10] 2 5 20 3 4 8 6 T2 cout << "Please enter " << X << " integer elements of the second array." << endl; for(int count = 0; count < X; ++count) { cout << "array2[" << count << "]: "; cin >> T2[count]; } X = 10

4 Function 1 : Enter values T1 & T2
void inputFunc(int T1[], int T2[], int X) { cout<<"Please enter "<<X<<" integer elements of the first array."<<endl; for(int count = 0; count < X; ++count) { cout << "array1[" << count << "]: "; cin >> T1[count]; } cout <<"Please enter "<<X<<" integer elements of the 2nd array."<<endl; { cout << "array2[" << count << "]: "; cin >> T2[count]; Passing array to function

5 Function 2 : Calculation
3 5 1 4 6 11 2 33 T1 2 5 20 3 4 8 6 T2 5 10 21 7 4 19 8 36 6 25 20 12 3 24 88 99 T3[0] =T1[0]+T2[0] T3[10]=T1[0]*T2[0] T3[1] =T1[1]+T2[1] T3[11]=T1[1]*T2[1] T3[i] =T1[i]+T2[i] T3[i+10]=T1[i]*T2[i]

6 Function 2 : Calculation
void calc(int T1[],int T2[],int T3[], int X) { for (int i = 0; i < X; i++) T3[i]=T1[i]+T2[i]; T3[i+10]=T1[i]*T2[i]; } Here both operation will happen at the same time one after the other

7 Function 3 : Display array
void printarray (int T[], int X) { for (int i=0; i<2*X; ++i) cout << T[i] << "\t"; cout << endl; } \t : horizontal tab (move cursor to next tab)

8 Main function int main () { const int X = 10;
int T1[X];int T2[X];int T3[2*X]; inputFunc(T1, T2, X); calc(T1, T2, T3, X); printarray(T3, X); return 0; }

9 Example 2 Ask the user to type10 integers of an array T.
Find and display the smallest and the greatest value in T. Find and display the frequency of each element of T.

10 Function 1 : Enter values T
void inputFunc(int T[], int X) { cout << "Please enter " << X << " integer elements of an array." << endl; for(int count = 0; count < X; ++count) cout << "array[" << count << "]: "; cin >> T[count]; } X = 10

11 Function 2:frequency of each element
Compare each T[i] with all the elements of T i : 0 to 9 compare T[i] with T[0], T[1],……, T[9] j : 0 to 9.

12 Function 2:frequency of each element
3 5 4 1 Frequency T[0] =T[0] : Frequency [0] = Frequency[0] + 1 T[0] ! =T[1] : T[0] =T[2] : Frequency [0] = Frequency[0] + 1 ……….. j from 0 to 9 T[0] =T[j] : if true : Frequency [0] = Frequency[0] + 1 ……….. T[0] !=T[9] : 4

13 Function 2:frequency of each element
3 5 4 1 Frequency 4 T[1] !=T[0] : T[1] =T[1] : Frequency [1] = Frequency[1] + 1 T[1] ! =T[2] : ……….. j from 0 to 9 T[1] =T[j] : if true :Frequency [1] = Frequency[1] + 1 ……….. T[1] =T[9] :Frequency [1] = Frequency[1] + 1 4 3

14 Function 2:frequency of each element
i from 0 to 9 T 3 5 4 1 j from 0 to 9 if T[i] =T[j] Frequency [i] = Frequency[i] + 1 Finding frequency

15 Function 3:Print elements of the array
void printarray (int T[], int X) { for (int i=0; i<X; ++i) cout << T[i] << "\t"; cout << endl; } \t : horizontal tab (move cursor to next tab) Print array T and Frequency array


Download ppt "Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements."

Similar presentations


Ads by Google