dr. soha s. zaghloul2 let arr be an array of 20 integers. write a complete program that first fills...

27
EXAMPLES ON ARRAYS

Upload: charla-bryan

Post on 14-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

EXAMPLES ON ARRAYS

Page 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

1. EXAMPLE (1)

Dr. Soha S. Zaghloul 2

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 maximum3) Find the subscript of the largest item (let it be sub)

Always store the subscript of the current max4) Display (print) max and sub

Page 3: 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

1. EXAMPLE (1) – SOLUTION

Dr. Soha S. Zaghloul 3

#include <stdio.h>int main(void){ return 0;} // end main

#include <stdio.h>int main(void){ // declaration part int arr[20];

return 0;} // end main

Page 4: 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

1. EXAMPLE (1) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 4

#include <stdio.h>#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

Page 5: 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

1. EXAMPLE (1) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 5

Required:1) Fill the array with up to 20 values

Use sentinel Condition depends on sentinel and the array

size

#include <stdio.h>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

Page 6: 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

1. EXAMPLE (1) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 6

Required:2) Find the maximum item in arr (let it be max)

Assume the first element is the maximum

#include <stdio.h>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 forreturn 0;} // end main

Page 7: 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

Required:3) Find the subscript of the largest item (let it be sub)

Always store the subscript of the current max

1. EXAMPLE (1) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 7

#include <stdio.h>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 forreturn 0;} // end main

Page 8: 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

Required:4) Display (print) max and sub

1. EXAMPLE (1) – SOLUTION (CONT’D)

#include <stdio.h>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. Zaghloul 8

Page 9: 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

1. EXAMPLE (1) – COMPLETE SOLUTION

#include <stdio.h>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. Zaghloul 9

What if the user didn’t enter any elements in the array?

items = 0 arr[0] is not defined

Page 10: 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

1. EXAMPLE (1) – REFINEMENTS

#include <stdio.h>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. Zaghloul 10

Do you want to accept upper and lower case ‘y’?

Page 11: 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

2. EXAMPLE (2)

Dr. Soha S. Zaghloul 11

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 far3) Arrays word and guessed have the same size4) Array guessed is initialized with ‘*’

Page 12: 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

2. EXAMPLE (2) (CONT’D)

Dr. Soha S. Zaghloul 12

Required:1) Store the word to be guessed in word

Fill in word with the target word2) 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

Page 13: 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

0 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

K I N G ~ S A U D ~ U N I V E R S I T Y

word

itemsSIZE

* * * * * * * * * * * * * * * * * * * *

guessed

letter= ‘G’

G

letter= ‘I’

I

letter= ‘B’ trials++

Page 14: 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

2. EXAMPLE (2) – SOLUTION

Dr. Soha S. Zaghloul 14

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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

Page 15: 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

2. EXAMPLE (2) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 15

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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 letterprintf (“Guess a letter> “);scanf (”%c”, &letter);return 0;} // end main

Page 16: 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

2. EXAMPLE (2) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 16

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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 letterprintf (“Guess a letter> “);scanf (”%c”, &letter);// Search for letter in wordwhile ((!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

Page 17: 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

2. EXAMPLE (2) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 17

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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 wordwhile ((!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 guessedelse if(trials <= LIVES) trials++; else printf (“You are out of lives. Computer wins!!!”);return 0;} // end main

Page 18: 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

2. EXAMPLE (2) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 18

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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 wordif (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 LIVESreturn 0;} // end main

Page 19: 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

2. EXAMPLE (2) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 19

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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 LIVESreturn 0;} // end main

The game should be repeated until:- The player wins

- The player looses- The player exits

Page 20: 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

2. EXAMPLE (2) – COMPLETE SOLUTION (1)

Dr. Soha S. Zaghloul 20

#include <stdio.h>#define SIZE 30 // array size#define LIVES 5 // maximum number of false trialsint 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] = ‘*’;

Page 21: 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

2. EXAMPLE (2) – COMPLETE SOLUTION (2)

Dr. Soha S. Zaghloul 21

// 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) ….

Page 22: 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

2. EXAMPLE (2) – COMPLETE SOLUTION (3)

Dr. Soha S. Zaghloul 22

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

Page 23: 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

3. EXAMPLE (3)

Dr. Soha S. Zaghloul 23

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}++;

Page 24: 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

3. EXAMPLE (3) – SOLUTION

Dr. Soha S. Zaghloul 24

#include <stdio.h>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

Page 25: 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

3. EXAMPLE (3) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 25

#include <stdio.h>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

Page 26: 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

3. EXAMPLE (3) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 26

#include <stdio.h>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

Page 27: 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

3. EXAMPLE (3) – SOLUTION (CONT’D)

Dr. Soha S. Zaghloul 27

#include <stdio.h>

#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