Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.

Similar presentations


Presentation on theme: "Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program."— Presentation transcript:

1

2 Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program finds and displays the following: 1) The largest item in arr 2) The subscript of the largest item. Given: arr is an array of 20 integers. Required: 1)Fill the array with up to 20 values  Use sentinel  Condition depends on sentinel and the array size 2) Find the maximum item in arr (let it be max)  Assume the first element is the maximum 3) Find the subscript of the largest item (let it be sub)  Always store the subscript of the current max 4) Display (print) max and sub

3 Dr. Soha S. Zaghloul3 #include int main(void) { return 0; } // end main #include int main(void) { // declaration part int arr[20]; return 0; } // end main

4 Dr. Soha S. Zaghloul4 #include #define SENTINEL /* SENTINEL should be of the same type of the data that the user enters. So, it must be an integer. Any integer could be part of the data. So, it is better to use a flag.*/ int main(void) { // declaration part int arr[20]; char input = ‘Y’; //this concept is called a flag return 0; } // end main Required: 1)Fill the array with up to 20 values  Use sentinel  Condition depends on sentinel and the array size

5 Dr. Soha S. Zaghloul5 Required: 1)Fill the array with up to 20 values  Use sentinel  Condition depends on sentinel and the array size #include int main(void) { // declaration part int arr[20]; char input = ‘Y’; int items = 0; // to record the current number of elements in arr // Input data as long as the user needs AND the array size is not exceeded while (input ==‘Y’) && (items < 20) { printf (“Do you want to enter more items? (Y/N)”); scanf (“%c”, &input); if (input == ‘Y’) { printf (“Enter an integer”); scanf(“%d”, arr[items]); items++; // add 1 to the subscript } // end if(input == ‘Y’) } // end while (…… /* when we exit the while loop, items hold the number of elements stored in the array -1 */ return 0; } // end main

6 Dr. Soha S. Zaghloul6 Required: 2) Find the maximum item in arr (let it be max)  Assume the first element is the maximum #include int main(void) { // declaration part int arr[20]; char input = ‘y’; int items = 0; // to record the current number of elements in arr int i; int max; // to store the maximum value // input data as long as the user needs and the array size is not exceeded while (input ==‘y’) && (items < 20) { printf (“do you want to enter more items? (y/n)”); scanf (“%c”, &input); if (input == ‘y’) { printf (“enter an integer”); scanf(“%d”, arr[items]); items++; // add 1 to the subscript } // end if(input == ‘y’) } // end while (…… max = arr[0]; for (i=0; i < items; i++) { if (arr[i] > max) max = arr[i]; } // end for return 0; } // end main

7 Required: 3) Find the subscript of the largest item (let it be sub)  Always store the subscript of the current max Dr. Soha S. Zaghloul7 #include int main(void) { // declaration part int arr[20]; char input = ‘y’; int items = 0; // to record the current number of elements in arr int sub; // subscript of max int i, max; // to store the maximum value // input data as long as the user needs and the array size is not exceeded while (input ==‘y’) && (items < 20) { printf (“do you want to enter more items? (y/n)”); scanf (“%c”, &input); if (input == ‘y’) { printf (“enter an integer”); scanf(“%d”, arr[items]); items++; // add 1 to the subscript } // end if(input == ‘y’) } // end while (…… max = arr[0]; sub = 0; for (i=0; i < items; i++) { if (arr[i] > max) { max = arr[i]; sub = i; } // end if } // end for return 0; } // end main

8 Required: 4) Display (print) max and sub #include int main(void) { // declaration part int arr[20]; char input = ‘y’; int items = 0; // to record the current number of elements in arr int sub; // subscript of max int i, max; // to store the maximum value // input data as long as the user needs and the array size is not exceeded while (input ==‘y’) && (items < 20) { printf (“do you want to enter more items? (y/n)”); scanf (“%c”, &input); if (input == ‘y’) { printf (“enter an integer”); scanf(“%d”, arr[items]); items++; // add 1 to the subscript } // end if(input == ‘y’) } // end while (…… max = arr[0]; sub = 0; for (i=0; i < items; i++) { if (arr[i] > max) { max = arr[i]; sub = i; } // end if } // end for printf (“the maximum element is arr[%d] = %d”, sub, arr[sub]); return 0; } // end main Dr. Soha S. Zaghloul8

9 #include int main(void) { // declaration part int arr[20]; char input = ‘y’; int items = 0; // to record the current number of elements in arr int sub; // subscript of max int i, max; // to store the maximum value while (input ==‘y’) && (items < 20) { printf (“do you want to enter more items? (y/n)”); scanf (“%c”, &input); if (input == ‘y’) { printf (“enter an integer”); scanf(“%d”, arr[items]); items++; // add 1 to the subscript } // end if(input == ‘y’) } // end while (…… max = arr[0]; sub = 0; for (i=0; i < items; i++) { if (arr[i] > max) { max = arr[i]; sub = i; } // end if } // end for printf (“the maximum element is arr[%d] = %d”, sub, arr[sub]); return 0; } // end main Dr. Soha S. Zaghloul9 What if the user didn’t enter any elements in the array?  items = 0  arr[0] is not defined

10 #include int main(void) { // declaration part int arr[20]; char input = ‘y’; int items = 0; // to record the current number of elements in arr int sub; // subscript of max int i, max; // to store the maximum value while (input ==‘y’) && (items < 20) { printf (“do you want to enter more items? (y/n)”); scanf (“%c”, &input); if ((input == ‘y’) || (input == ‘Y’)) { printf (“enter an integer”); scanf(“%d”, arr[items]); items++; // add 1 to the subscript } // end if(input == ‘y’) } // end while (…… if (items > 0) { max = arr[0]; sub = 0; for (i=0; i < items; i++) { if (arr[i] > max) { max = arr[i]; sub = i; } // end if } // end for printf (“the maximum element is arr[%d] = %d”, sub, arr[sub]); } // end if (items > 0) else printf (“No data is entered\n”); return 0; } // end main Dr. Soha S. Zaghloul10 Do you want to accept upper and lower case ‘y’?

11 Dr. Soha S. Zaghloul11 Write an interactive program that plays a game of hangman. Store the word to be guessed in an array of characters called word. The player should guess the letters belonging to word. The program should terminate when either: 1) All letters have been guessed correctly (the player wins) or 2) A specified number of incorrect guesses have been made (the computer wins). Hint: -Use another array guessed to keep track of the solution so far. -Initialize all elements of guessed with ‘*’ -Each time a letter in word is guessed, replace the corresponding ‘*’ in guessed with that letter Given: 1)Array word to store the target word. 2)Array guessed to keep track of the correct guesses so far 3)Arrays word and guessed have the same size 4)Array guessed is initialized with ‘*’

12 Dr. Soha S. Zaghloul12 Required: 1)Store the word to be guessed in word  Fill in word with the target word 2) Initialize guessed with ‘*’  for(i=0; i<size; i++) guessed[i] = ‘*’; 3) The player should guess the letters belonging to word  scanf (“%c”, letter);  Search letter in word  If found, then write the letter in the corresponding subscript of guessed  If not found, increment lives (number of false guesses) 4) Repeat the program until a. all letters are guessed  There is no ‘*’ in guessed b. number of lives exceeds a predefined value  trials = LIVES

13 0123456789 1011121314151617181920212223242526272829 KING~SAUD~UNIVERSITY word items SIZE ******************** guessed letter= ‘G’ G letter= ‘I’ I letter= ‘B’ trials++

14 Dr. Soha S. Zaghloul14 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char word[SIZE], guessed[SIZE]; int i; // loop counter char input = ‘Y’; int items = 0;// number of letters to be filled // initialize guessed with ‘*’ for (i= 0; i< SIZE; i++) guessed[i] = ‘*’; // fill in word with letters (Refer to example 1) while (input ==‘Y’) && (items < SIZE) { printf (“Do you want to enter more items? (Y/N)”); scanf (“%c”, &input); if (input == ‘Y’) { printf (“Enter a letter”); scanf(“%c”, word[items]); items++; // add 1 to the subscript } // end if(input == ‘Y’) } // end while (…… return 0; } // end main

15 Dr. Soha S. Zaghloul15 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char word[SIZE], guessed[SIZE]; char letter;// The player’s guess int i, items = 0; char input = ‘Y’; // initialize guessed with ‘*’ for (i= 0; i< SIZE; i++) guessed[i] = ‘*’; // fill in word with letters (Refer to example 1) while (input ==‘Y’) && (items < SIZE) { printf (“Do you want to enter more items? (Y/N)”); scanf (“%c”, &input); if (input == ‘Y’) { printf (“Enter a letter”); scanf(“%c”, word[items]); items++; // add 1 to the subscript } // end if(input == ‘Y’) } // end while (…… // Let the player guess a letter printf (“Guess a letter> “); scanf (”%c”, &letter); return 0; } // end main

16 Dr. Soha S. Zaghloul16 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char letter, input = ‘Y’, word[SIZE], guessed[SIZE]; int i, items = 0; int found = 0;// flag initiated to false // initialize guessed with ‘*’ // fill in word with letters // Let the player guess a letter printf (“Guess a letter> “); scanf (”%c”, &letter); // Search for letter in word while ((!found) && (i <= items))// while not found ie. while found == 0 { if(letter == word[i]) found =1;// set the flag to 1 else i++;// increment i to check the next subscript } // end while ((!found) …. return 0; } // end main

17 Dr. Soha S. Zaghloul17 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char letter, input = ‘Y’, word[SIZE], guessed[SIZE]; int i, items, found = 0; int trials = 0;// number of false trials // initialize guessed with ‘*’ // fill in word with letters // Let the player guess a letter // Search for letter in word while ((!found) && (i <= items))// while not found ie. while found == 0 { if(letter == word[i]) found =1;// set the flag to 1 else i++;// increment i to check the next subscript } // end while ((!found) …. if (found)// ie. if (found == 1) guessed[i] = letter; // store the correct letter in the corresponding position in guessed else if(trials <= LIVES) trials++; else printf (“You are out of lives. Computer wins!!!”); return 0; } // end main

18 Dr. Soha S. Zaghloul18 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char letter, input = ‘Y’, word[SIZE], guessed[SIZE]; int i, items, found = 0, trials = 0; int win = 1;// flag to identify that the player wins initialized to true (1) // initialize guessed with ‘*’ // fill in word with letters // Let the player guess a letter // Search for letter in word if (found)// ie. if (found == 1) { guessed[i] = letter; // store the correct letter in the corresponding position in guessed i = 0; while ((win) && (i <= items)) { if (guessed[i] != ‘*’) i++; // check the next location else win = 0; // since there is one ‘*’ in guessed, the game is not finished yet } // end while if (win) /* all elements are not equal to ‘*’. We exited the while loop because i reached items */ printf (“Hurrrrrrah…you win!!!”); } // end if(found) // else check if trials exceeded LIVES return 0; } // end main

19 Dr. Soha S. Zaghloul19 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char letter, input = ‘Y’, word[SIZE], guessed[SIZE]; int i, items, found = 0, trials = 0, win = 1; // initialize guessed with ‘*’ // fill in word with letters // Let the player guess a letter // Search for letter in word // if (found):- store the correct letter in the corresponding position in guessed // - check if the player wins // else (not found) :- increment trials with 1 // - check if trials exceeded LIVES return 0; } // end main The game should be repeated until: -The player wins -The player looses -The player exits

20 Dr. Soha S. Zaghloul20 #include #define SIZE 30// array size #define LIVES5 // maximum number of false trials int main(void) {//declaration part char letter, input = ‘Y’, word[SIZE], guessed[SIZE]; int i, items, found = 0, trials = 0, win = 1; char exit; int end = 0; // flag to mark the end of the game initially set to false (0) while (!end) { printf (“Do you want to play more? (Y/N)”); scanf (“%c”, &exit); if (exit != ‘Y’) // the player exits break; // initialize guessed with ‘*’ for (i= 0; i< SIZE; i++) guessed[i] = ‘*’;

21 Dr. Soha S. Zaghloul21 // fill in word with letters while (input ==‘Y’) && (items < SIZE) { printf (“Do you want to enter more items? (Y/N)”); scanf (“%c”, &input); if (input == ‘Y’) { printf (“Enter a letter”); scanf(“%c”, word[items]); items++; // add 1 to the subscript } // end if(input == ‘Y’) } // end while (…… // Let the player guess a letter printf (“Guess a letter> “); scanf (”%c”, &letter); // Search for letter in word while ((!found) && (i <= items))// while not found ie. while found == 0 { if(letter == word[i]) found =1;// set the flag to 1 else i++;// increment i to check the next subscript } // end while ((!found) ….

22 Dr. Soha S. Zaghloul22 if (found)// ie. if (found == 1) { guessed[i] = letter; i = 0; while ((win) && (i <= items)) { if (guessed[i] != ‘*’) i++; // check the next location else win = 0; // since there is one ‘*’ in guessed, the game is not finished yet } // end while ((win)… if (win) { printf (“Hurrrrrrah…you win!!!”); end = 1;// the player wins } } // end if(found) else if(trials <= LIVES) trials++; else { printf (“You are out of lives. Computer wins!!!”); end = 1; // the player looses } // end else…if (trials <= LIVES) } // end while (!end) return 0; } // end main

23 Dr. Soha S. Zaghloul23 Write a complete program that generates a report about the number of student earning each grade (A, B, C, D, F). The instructor will enter the grade, and the program will update the corresponding counter accordingly. Given: counter is an array of 5 elements. Required: 1)The instructor inputs a grade  scanf (“%c”, &grade) 2)Update the counter in the array accordingly as follows: a.If the grade is A  increment counter[0]  counter[0]++; b.If the grade is B  increment counter[1]  counter[1]++; c.If the grade is C  increment counter[2]  counter[2]++; d.If the grade is D  increment counter[3]  counter[3]++’ e.If the grade is F  increment counter[4]  counter[4}++;

24 Dr. Soha S. Zaghloul24 #include int main(void) { int i;// the loop control variable (for counter) char grade; // grade entered by the user int counter[5]; // array of counters // initialize the array of counters with zeroes for (i=0; i<5; i++) counter[i] = 0; return 0; } // end main

25 Dr. Soha S. Zaghloul25 #include int main(void) { int i;// the loop control variable (for counter) char grade; // grade entered by the user int counter[5]; // array of counters // initialize the array of counters with zeroes for (i=0; i<5; i++) counter[i] = 0; // get the grade from the instructor printf (“enter next grade”); scanf (“%c”, &grade); return 0; } // end main

26 Dr. Soha S. Zaghloul26 #include int main(void) { char grade; // grade entered by the user int i, counter[5]; // array of counters // initialize the array of counters with zeroes for (i=0; i<5; i++) counter[i] = 0; // get the grade from the instructor printf (“enter next grade”); scanf (“%c”, &grade); // update the corresponding counter accordingly switch (grade) { case ‘A’: counter[0]++; break; case ‘B’: counter[1]++; break; case ‘C’: counter[2]++; break; case ‘D’: counter[3]++; break; case ‘F’: counter[4]++; break; default: printf (“Invalid input\n”); } // end switch return 0; } // end main

27 Dr. Soha S. Zaghloul27 #include #define SENTINEL ‘X’ int main(void) { char grade = ‘E’ ; // initialize with any invalid value int i, counter[5]; // array of counters // initialize the array of counters with zeroes for (i=0; i<5; i++) counter[i] = 0; while (grade != SENTINEL) { // get the grade from the instructor printf (“enter next grade %c to exit”, SENTINEL); scanf (“%c”, &grade); // update the corresponding counter accordingly switch (grade) { case ‘A’: counter[0]++; break; case ‘B’: counter[1]++; break; case ‘C’: counter[2]++; break; case ‘D’: counter[3]++; break; case ‘F’: counter[4]++; break; case ‘X’: break; default: printf (“Invalid input\n”); } // end switch } // end while return 0; } // end main


Download ppt "Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program."

Similar presentations


Ads by Google