08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8 ■ Arrays

fprintf(out, "\nThe winner(s)\n");

int win = getLargest(vote, 1, MaxCandidates);

int winningVote = vote[win];

for (int c = 1; c <= MaxCandidates; c++)

if (vote[c] == winningVote) fprintf(out, "%s\n", name[c]);

} //end printResults

EXERCISES 8

1. Explain the difference between a simple variable and an array variable.

2. Write array declarations for each of the following: (a) a floating-point array of size 25

(b) an integer array of size 50 (c) a character array of size 32.

3. What is a subscript? Name three ways in which we can write a subscript.

4. What values are stored in an array when it is first declared?

5. Name two ways in which we can store a value in an array element.

6. Write a function which, given a number from 1 to 12 and a character array, stores

the name of the month in the array. For example, given 8, it stores August in the

array. Store the empty string if the number given is not valid.

7. You declare an array of size 500. Must you store values in all elements of the array?

8. Write code to read 200 names from a file and store them in an array.

9. An array num is of size 100. You are given two values i and k, with

0 £ i < k £ 99. Write code to find the average of the numbers from num[i] to

num[k], inclusive.

10. Write a function, which, given a string of arbitrary characters, returns the number of

consonants in the string.

11. Modify the letter frequency count program (Program P8.2) to count the number of

non-letters as well. Make sure you do not count the end-of-line characters.

12. Write a function that, given an array of integers and an integer n, reverses the first n

elements of the array.

13. Write a program to read names and phone numbers into two arrays. Request a

name and print the person’s phone number. Use at least one function.

14. Write a function indexOfthat, given a string s and a character c, returns the

position of the first occurrence of c in s. If c is not in s, return -1. For example,

indexOf("brother",'h') returns 4 but indexOf("brother", 'a') returns -1.

15. Write a function substringthat, given two strings s1 and s2, returns the

starting position of the first occurrence of s1 in s2. If s1 is not in s2, return -1.

For example, substring("mom","thermometer") returns 4 but

substring("dad","thermometer") returns -1.

241

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!