NAME : HIBA IMRAN
ID No. : iu05-0325-0729
FACULTY: GHAZALA SHAFI SHEIKH
COURSE : PROGRAMMING FUNDAMENTALS (THEORY)
QUESTION NO 1:
7.1: Define the following arrays:
A) empNums, a 100-element array of int s
B) payRates, a 25-element array of float s
C) miles, a 14-element array of long s
D) cityName, a 26-element array of string objects
E) lightYears, a 1,000-element array of double s
a) int empNums[100];
b) float payRates[25];
c) long miles[14];
d) char cityName[26];
e) Double lightYears[1000];
QUESTION NO 2:
7.2: What’s wrong with the following array definitions?
int readings[-1];
float measurements[4.5];
int size;
string names[size];
1. int readings[-1];
Problem:
Array size cannot be negative.
Reason:
An array must have a positive number of elements.
2. float measurements[4.5];
Problem:
Array size cannot be a decimal number.
Reason:
Array size must be a whole number (integer).
3. array;
Problem:
size is not given a value before being used.
Reason:
Array size must be known at compile time (fixed value), not an
uninitialized variable.
7.3: What would the valid subscript values be in a four-
element array of double s?
In a four-element array of doubles, the valid subscript values are 0, 1, 2,
and 3.
7.8: Define the following arrays:
A) ages, a 10-element array of ints initialized with the
values 5, 7, 9, 14, 15, 17, 18, 19, 21, and 23.
B) temps, a 7-element array of floats initialized with the
values 14.7, 16.3, 18.43, 21.09, 17.9, 18.76, and 26.7.
C) alpha, an 8-element array of chars initialized with the
values ‘J’, ‘B’, ‘L’, ‘A’, ‘*’, ‘$’, ‘H’, and ‘M’
a) int ages[10] = {5, 7, 9, 14, 15, 17, 18, 19, 21, 23};
b) float temps[7] = {14.7, 16.3, 18.43, 21.09, 17.9, 18.76, 26.7};
c) char alpha[8] = {'J', 'B', 'L', 'A', '*', '$', 'H', 'M'};
7.9: Is each of the following a valid or invalid array definition?
(If a definition is invalid, explain why.)
int numbers[10] = {0, 0, 1, 0, 0, 1, 0,
0, 1, 1};
int matrix[5] = {1, 2, 3, 4, 5, 6, 7};
double radii[10] = {3.2, 4.7};
int table[7] = {2, , , 27, , 45, 39};
char codes[] = {'A', 'X', '1', '2',
's'};
int blanks[];
7.19: Define a two-dimensional array of int s named grades . It
should have 30 rows and 10 columns.
int grades[30][20];
7.23: Define a two-dimensional array named settings large
enough to hold the table of data below. Initialize the array with
the values in the table.
12 24 32 21 42
14 67 87 65 90
19 1 24 12 8
int settings[3][5]={
{ 12 ,24, 32, 21, 42},
{4 ,67, 87, 65, 90},
{19 , 1, 24 , 12, 8} };
7.24: Fill in the table below so it shows the contents of the
following array:
int table[3][4] = {{2, 3}, {7, 9, 2}, {1}};
Row Col 0 Col 1 Col 2 Col 3
0 2 3 0 0
1 7 9 2 0
2 1 0 0 0
2 3 0 0
7 9 2 0
1 0 0 0