Presentation is loading. Please wait.

Presentation is loading. Please wait.

EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.

Similar presentations


Presentation on theme: "EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100."— Presentation transcript:

1 EXAMPLE

2 Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100. The subscripts are to be stored in another array Y of the same size. The array X is filled by the user. The array Y is initialized to -1.

3 Dr. Soha S. Zaghloul3 #include int main (void) { int X[100], int Y[100], sub, subY; // fill and initialize the arrays for (sub = 0; sub < 100; sub++) { printf (“Enter elements of X> “); scanf (“%d”, &X[sub]); Y[sub] = -1; } // end for // get the subscripts of multiples of 7 in X, and store them in Y subY = 0; //use another subscript subY for Y and initializes subY for (sub = 0; sub < 100; sub++) { if (X[sub]%7 == 0) { Y[subY] = sub; subY++; } //end if } // end for } // end main

4 Dr. Soha S. Zaghloul4 #include int main (void) { int X[100], int Y[100], sub, subY; subY = 0;// intialize the subscript of Y for (sub = 0; sub < 100; sub++) { printf (“Enter elements of X> “); scanf (“%d”, &X[sub]); Y[sub] = -1; if (X[sub]%7 == 0) { Y[subY] = sub; subY++; } //end if } // end for } // end main

5 Dr. Soha S. Zaghloul5 Write a complete modular program that contains the following functions: 1.Function InitArray that initializes an array with the same value for all elements. The value is of type int. InitArray accepts an array of type integer, its size, and the initial value. 2.Function FillArray that fills an array with values from the user. FillArray accepts an array of type integer and its size. 3.Function Mod7occ (Mod 7 occurrences) that searches for all the elements that are multiple of 7 in an array. The subscripts are to be stored in another array of the same size. Mod7occ accepts two arrays of type integer and of the same size. One of the arrays is accepted as an input parameter.

6 Dr. Soha S. Zaghloul6 void FillArray (int list[], int size) { int i; for (i = 0; i < size; i++) { printf (“Enter the next array element> “); scanf (“%d”, &list[i]); } // end for } // end FillArray

7 Dr. Soha S. Zaghloul7 void InitArray (int list[], int size, int value) { int i; for (i = 0; i < size; i++) list[i] = value; } // end InitArray

8 Dr. Soha S. Zaghloul8 void Mod7occ (const int list1[], int list2[]) { int i, j;// i is used as a subscript to list 1, j is used as a subscript to list2 for (i = 0; i < size; i++) if (list1[i] % 7 == 0) { list2[j] = i; j++; } // end for } // end Mod7occ

9 Dr. Soha S. Zaghloul9 void Modnocc (const int list1[], int list2[], int n) { int i, j;// i is used as a subscript to list 1, j is used as a subscript to list2 for (i = 0; i < size; i++) if (list1[i] % n == 0) { list2[j] = i; j++; } // end for } // end Modnocc Mod7occ may be generalized to search for the multiples of any number n. n will be specified by the user. We will call this Modnocc.

10 Dr. Soha S. Zaghloul10 #include #defineSIZE100 // Prototypes void FillArray (int list[], int size); void InitArray (int list[], int size, int value); void Modnocc (const int list1[], int list2[], int n); // main program int main (void) { int X[SIZE], Y[SIZE]; int init = -1;// value used to initialize Y int num = 7;// search for mod num. You can also get the value through scanf int sub; // used as the subscript to print Y // calling functions FillArray (X, SIZE); InitArray (Y, SIZE, init); Modnocc(X, Y, num); // printing Y for (sub = 0; sub < SIZE; sub++) printf (“%d \n”, Y[sub]); } // end main // Functions Definition void FillArray (int list[], int size) {--- } //end of FillArray void InitArray (int list[], int size, int value) {--- } // end of InitArray void Modnocc (const int list1[], int list2[], int n) { --- } // end of Modnocc

11 Dr. Soha S. Zaghloul11 Write a function MultiScalar to multiply a two-dimensional array by a scalar. The array and the scalar are of type integer. void MultiScalar (int rows, int cols, int list[rows][cols], int scalar) { int i;// i is used as a counter to the rows int j; // j is used as a counter to the columns for (i = 0; i < rows; i++) for (j = 0; j < cols; j++) list[i][j] = list[i][j] * scalar; } // end MultiScalar Note that: 1.we must identify the number of rows and columns of a two- dimensional array in the function header. 2.The number of rows and columns are defined in the function header before defining the two-dimensional array. 3.An element of a two-dimensional array is referred to by list[i][j] not list[i,j]

12 Dr. Soha S. Zaghloul12 Write a function DisplayMat to display a two-dimensional matrix. void DisplayMat (int rows, int cols, const int list[rows][cols]) { int i;// i is used as a counter to the rows int j; // j is used as a counter to the columns for (i = 0; i < rows; i++)// loop through the rows for (j = 0; j < cols; j++) // loop through the columns { printf (“%d\t”, list[i][j]);// leave a tab between elements in the same row printf (“\n”); // start a new line for a new row } // end DisplayMat

13 Dr. Soha S. Zaghloul13 Write a function MatProduct to get the product of all elements of a two- dimensional array. The array is of type integer. Write a function MatManipulate that manipulates a two-dimensional array. In addition to the input array, the function accepts an output array to store the results, an integer scalar, a character operation that specifies the arithmetic operation to be performed between the scalar and the array.


Download ppt "EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100."

Similar presentations


Ads by Google