C Programming Week5
C Programming Week5
An Array
• A data structure containing items of same data type
• Declaration: array name, storage reservation
– int marks[7] = {22,15,75,56,10,33,45};
CS1100 • a contiguous group of memory locations
22 0
15 1
Introduction to Programming • named “marks” for holding 7 integer items
75 2
– elements/components - variables
Arrays • marks[0], marks[1], … , marks[6] 56 3
Example using Arrays Counting Digits in Text (Kenighan & Ritchie, pp. 59)
Read ten numbers into an array and compute their average #include<stdio.h>
An array of ten integers
#include <stdio.h> int main( ){
int main( ){
int numbers[10], sum = 0, i; int c, i, nWhite, nOther, nDigit[10];
float average; nWhite = nOther = 0;
for ( i = 0; i < 10; i++) for(i=0;i<10;i++)
scanf(“%d”, &numbers[i]); nDigit[i]=0;
for ( i = 0; i < 10; i++) while((c = getchar( ))! = EOF){
sum = sum + numbers[i];
switch(c){
average = (float) sum/10;
printf(“The average of numbers is: %f”, average); case ‘0’:case ‘1’:case ‘2’:case ‘3’:case ‘4’:case ‘5’:
return 0; /* should be there in all programs */ case ‘6’:case ‘7’:case ‘8’:case ‘9’: nDigit[c- ‘0’]++;
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M
break; 4
1
15/09/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8
Data, Types, Sizes, Values The Char, Signed and Unsigned Types
• int, char, float, double • Qualifier signed or unsigned can be applied to int
• char – one byte, capable of holding one character or char
• int – an integer, different kinds exist! • Unsigned numbers are non-negative
– Integer Qualifiers – short and long • Signed char holds numbers between –128 and 127
– short int – 16 bits, long int – 32 bits (Typical) – Whether char is signed or unsigned depends on the
– Size is compiler dependant system. Find out on your system.
• based on the underlying hardware – Print integers between 0 to 255 as characters, (and also
– int is at least 16 bits, short is at least 16 bits, long is at integers between –128 to 127) on your system.
least 32 bits
– int is no larger than long and at least as long as short
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10
2
15/09/17
3
15/09/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M 20
SD, PSK, NSN, DK, TAG – CS&E, IIT M 21 SD, PSK, NSN, DK, TAG – CS&E, IIT M 22
4
15/09/17
printf("big = %ld and not %hd\n", big, big); • Arrays can be initialised when declared:
– char mesg[] = “hello”; //is a valid declaration
return 0;
– int numbers[5] = {0,1,2,3,4}; //is a valid declaration
}
• But an assignment to an array in the program is a
big = 65537 and not 1 syntax error
When the value 65537 (216 + 1) is written in binary format as a 32-bit
number, it looks like 00000000000000010000000000000001. Using
the %hd specifier persuaded printf() to look at just the last 16 bits;
SD, PSK, NSN, DK, TAG – CS&E, 29 30
therefore,
IIT M it displayed the value as 1. SD, PSK, NSN, DK, TAG – CS&E, IIT M
5
15/09/17
Exercise
• Write a program which will exit when a certain
number of occurrences of any keystroke is read.
– You need arrays
– Loops
– Loops with logical operations and so on.