$30
EECS 2031 Page 1 of 3
LAB 2 ─ Arrays and Control Structures
1. Problem A
1.1 Specification
Write a C program to count the number of digits (‘0’, ‘1’, ‘2’, ..., ‘8’, ‘9’) in a line of characters. The
program reads from the standard input a line of characters and outputs the number of digits found in
the line. Compute the sum of all the digits found and output that sum. If the input line contains no
digits, the sum is 0.
2.2 Implementation
The program should:
• be named lab2a.c
• use getchar and a loop to read a line of characters. The loop terminates when a new line
character ‘\n’ is entered.
• display the following prompt before each input line:
Enter a line of characters>
• display on the standard output the number of digits found in the input line and the sum of all
the digits, separated by a tab character ‘\t’.
2.3 Sample Inputs/Outputs:
indigo 352 % lab2a
Enter a line of characters>Welcome to CSE2031, “Software Tools”.
4 6
indigo 353 % lab2a
Enter a line of characters>0123456789
10 45
indigo 354 % lab2a
Enter a line of characters>a b c d e f g h
0 0
indigo 355 %
EECS 2031 Page 2 of 3
2. Problem B
2.1 Specification
Write a C program to input a set of integers, store them in an array, find the maximum and minimum
values of the set, and display those two values.
2.2 Implementation
• The program is named lab2b.c
• Allocate an array of size 100. The array size should be declared as a constant.
• Use a loop and scanf to read one integer at a time and place it into the array. The loop terminates
when the input is a zero. Store the zero in the array as well (i.e., the last element of an array is
always a zero).
• Display before each input the following prompt:
Enter the next array element>
• Find the maximum and minimum values of the set of integers stored in the array (including the last
element, which is a zero).
• Display the maximum and minimum values on the standard output, in that order and separated by a
tab character ‘\t’.
• Assume that all inputs are valid and that the number of input integers is less than 100.
2.3 Sample Inputs/Outputs
indigo 360 % lab2b
Enter the next array element>10
Enter the next array element>2
Enter the next array element>-34
Enter the next array element>15
Enter the next array element>-20
Enter the next array element>0
15 -34
indigo 361 % lab2b
Enter the next array element>-100
EECS 2031 Page 3 of 3
Enter the next array element>-20
Enter the next array element>0
0 -100
indigo 362 %
3. Problem C
Repeat problem A, but do not display the prompt “Enter a line of characters>”. Name this
program lab2c.c.
4. Problem D
Repeat problem B, but do not display the prompts “Enter the next array element>”. Name
this program lab2d.c.
Common Notes
All submitted files should contain the following header:
/***************************************
* EECS2031 – Lab 2
* Filename: Name of file
* Author: Last name, first name
* Email: Your preferred email address
* EECS login ID: Your EECS login ID
****************************************/
In addition, all programs should follow the following guidelines:
• Include the stdio.h library in the header of your .c files.
• Use printf to print text and outputs according to the required formats.
• End each output result with a new line character ‘\n’.
• Do not use any C library function except getchar(), putchar(), scanf() and
printf().
• Assume that all inputs are valid (no error checking is required on inputs).