init1
Write a C program init1.c that
- defines an
intarray of size ten; - and, using a
forloop, initialises each element to zero (i.e. arrayk ∈ {0..9} = 0); - uses another
forloop to print out the ten array elements.
| Main Contents Page | CTEC1401 Programming in C |
Quick Reference |
| Week 8: Iteration | Week 9: Arrays |
Week 10: Pointers I |
Before the lab session read CFTTP Chapter 7 and study the solved exercises very carefully.
Write a C program init1.c that
int array of size ten;
for loop, initialises each element to
zero (i.e. arrayk ∈ {0..9} = 0);
for loop to print out the ten array elements.
Write a C program init2.c that
int array of size ten;
for loop, initialises each element to the same
value as its index (i.e. arrayk ∈ {0..9} = k);
for loop to print out the ten array elements.
Write a C program init3.c that
int array of size ten;
for loop, initialises each element to zero
if its index is even and one if its index is odd;
for loop to print out the ten array elements.
Write a C program reverse1.c
that prompts the user and, using a for loop, reads in ten
int numbers and then, using another for loop,
prints them out in reverse order. You should use an array to store the
ten numbers.
Write a C program reverse2.c
that prompts the user and, using a for loop, reads in twenty
int numbers and then, using another for loop,
prints out the elements at even indexes in reverse order starting at
index 18 and finishing at index 2 (inclusive).
Suppose that the following declaration defines the number of times that each of the national lottery balls (1 ..49) has been drawn over a given period.
int lottery[49] = { 23,16,18,19,26,13,22, /* 1 .. 7 */
20,14,22,18,21,15,17, /* 8 .. 14 */
24,15,18,20,13,14,20, /* 15 .. 21 */
18,22,20,16,19,11,20, /* 22 .. 28 */
16,28,22,20,15,17,17, /* 29 .. 35 */
21,21,19,20,14,22,25, /* 36 .. 42 */
19,17,26,18,20,23,12 }; /* 43 .. 49 */
Write a program lottery1.c to print a histogram showing the
information graphically using stars like this:
1 (23) | *********************** 2 (16) | **************** 3 (18) | ****************** 4 (19) | ******************* 5 (26) | ************************** 6 (13) | ************* 7 (22) | ********************** 8 (20) | ******************** 9 (14) | ************** 10 (22) | ********************** 11 (18) | ****************** 12 (21) | ********************* 13 (15) | *************** 14 (17) | ***************** 15 (24) | ************************ 16 (15) | *************** 17 (18) | ****************** 18 (20) | ******************** 19 (13) | ************* 20 (14) | ************** 21 (20) | ******************** 22 (18) | ****************** 23 (22) | ********************** 24 (20) | ******************** 25 (16) | **************** 26 (19) | ******************* 27 (11) | *********** 28 (20) | ******************** 29 (16) | **************** 30 (28) | **************************** 31 (22) | ********************** 32 (20) | ******************** 33 (15) | *************** 34 (17) | ***************** 35 (17) | ***************** 36 (21) | ********************* 37 (21) | ********************* 38 (19) | ******************* 39 (20) | ******************** 40 (14) | ************** 41 (22) | ********************** 42 (25) | ************************* 43 (19) | ******************* 44 (17) | ***************** 45 (26) | ************************** 46 (18) | ****************** 47 (20) | ******************** 48 (23) | *********************** 49 (12) | ************
Rewrite your lottery1 program as
lottery2.c. However, the output should display the "bars"
using blocks enclosed by '_' (unerscores) and
'| (vertical bars) as show below. (Note, this is slightly
tricky):
_______________________
1 (23) |_______________________|
2 (16) |________________|_
3 (18) |__________________|
4 (19) |___________________|______
5 (26) |__________________________|
6 (13) |_____________|________
7 (22) |______________________|
8 (20) |____________________|
9 (14) |______________|_______
10 (22) |______________________|
11 (18) |__________________|__
12 (21) |_____________________|
13 (15) |_______________|_
14 (17) |_________________|______
15 (24) |________________________|
16 (15) |_______________|__
17 (18) |__________________|_
18 (20) |____________________|
19 (13) |_____________|
20 (14) |______________|_____
21 (20) |____________________|
22 (18) |__________________|___
23 (22) |______________________|
24 (20) |____________________|
25 (16) |________________|__
26 (19) |___________________|
27 (11) |___________|________
28 (20) |____________________|
29 (16) |________________|___________
30 (28) |____________________________|
31 (22) |______________________|
32 (20) |____________________|
33 (15) |_______________|_
34 (17) |_________________|
35 (17) |_________________|___
36 (21) |_____________________|
37 (21) |_____________________|
38 (19) |___________________|
39 (20) |____________________|
40 (14) |______________|_______
41 (22) |______________________|__
42 (25) |_________________________|
43 (19) |___________________|
44 (17) |_________________|________
45 (26) |__________________________|
46 (18) |__________________|_
47 (20) |____________________|__
48 (23) |_______________________|
48 (12) |____________|
The famous Fibonacci number sequence starts like this:
Each number in the sequence is the sum of the previous two.
Write a C program fibonacci.c that
fibs which will be used
to store the first forty Fibonacci numbers.
1 to each of the first two elements of
the array;
for loop to assign the correct values to the rest of
the array.
How far can you extend this sequence until the values are too
big to be stored using an int variable?
Create a text file called monthlysales.c that contains the following C program:
#include <stdio.h>
main()
{
int sales[12] = {100,90,120,150,160,170,170,120,140,100,100,90};
int count;
printf("Jan\tFeb\tMar\tApr\tMay\tJun\tJul\tAug\tSep\tOct\tNov\tDec\n");
for(count=0;count<12;count++)
{
printf("%d\t",sales[count]);
}
printf("\n");
}
Quarter 1: ????
Quarter 2: ????
Quarter 3: ????
Quarter 4: ????