Study questions covering C Chapters 8 - 10 Flashcards
Write a declaration of an array named weekend containing seven bool values. Include an initializer that
makes the first and last values true; all other values should be false.
bool b[7] = {[0]=true,[6]=true};
*Note: remember need to compile using c99 and include stdbool.h
Identify the error in the following C statement:
int x[8], i;
for(i = 0; i<=8; ++i)
x[i] = i;
Will the error be detected? If so, when?
The error is the condition in the for loop. Element numbers span from 0 to array length-1.
The error SHOULD be detected when i = 8 and the program tries to access element 8 of array x; which does not exist.
However, there is no bounds checking mechanism implemented in C to raise speed. “undefined behavior” results and no error will occur.
What is the output of the printf statement in the following portion of a C program?
{
int x[10]={0,5,10,15};
int k=1, sum;
for(sum=0;k, sum);
}
}
10
26
27
Write a program segment to display the sum of the values in each row of a 5×3 type double array named
table. How many row sums will be displayed? How many elements are included in each sum?
for a 5x3 type double array named table
double table[5][3];
int sum=0;
for (int rows=0;rows,rows,sum);
sums=0;
}
5 row sums will be displayed
3 elements included in each sum.
Re-answer the previous question for the column sums.
for a 5x3 type double array named table
double table[5][3];
int sum=0;
for(int cols=0;cols,cols,sum);
sums=0;
}
3 column sums will be displayed
5 elements included in each sum
The Febonacci number are 0, 1, 1, 2, 3, 5, 8, 13, … where each number is the sum of the two preceding
numbers. Write a program fragment that declares an array named fib_numbers of length 40 and fills the
array with the first 40 Febonacci numbers. Hint: Fill in the first two numbers individually, then use a loop
to compute the remaining numbers.
int fib_numbers[40]={0,1};
for(int i=2;i
Write a C program segment to display the index of the smallest and the largest numbers in an array x of 20
integers. Assume array x already have values assigned to each element.
int min=x[0],max=x[0];
for(int i=1;imax)
max=x[i];
}
printf(“The smallest number in the array is %d and the largest number is %d\n”,min,max);
Write a declaration for a two-dimensional array named temperature_readings that stores one
month of hourly temperature readings. For simplicity, assume that a month has 30 days. The rows of the
array should represent days of the month; the columns should represent hours of the day.
double temperature_readings[30][24]
Using the array of the above exercise, write a program fragment that computes the average temperature for a month (averaged over all days of the month and all hours of the day).
double temperature_readings[30][24];
double avrg=0;
for(int i=0;i,avrg);
Write a declaration for an 8×8 char array named chess_board. Include an initializer that puts the
following data into the array (one character per array element):
r n b q k b n r P p p p p p p p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . P p p p p p p p R N B Q K B N R
char chess_board[8][8]={'r','n','b','q','k','b','n','r', 'p','p','p','p','p','p','p','p', '.','.','.','.','.','.','.','.', '.','.','.','.','.','.','.','.', '.','.','.','.','.','.','.','.', '.','.','.','.','.','.','.','.', 'p','p','p','p','p','p','p','p', 'R','N','B','Q','K','B','N','R'};
Write a program fragment that declares an 8 × 8 char array named checker_board and then uses a
loop to store the following data into the array (one character per array element):
B R B R B R B R R B R B R B R B B R B R B R B R R B R B R B R B B R B R B R B R R B R B R B R B B R B R B R B R R B R B R B R B
Hint: The element in row i, column j, should be the letter B if i + j is an even number.
char checker_board[8][8];
for(int r=0;r;
}
}
Write a program that reads a 5×5 array of integers and then prints the row sums and the column sums:
Enter row 1: 8 3 9 0 10 Enter row 2: 3 5 17 1 1 Enter row 3: 2 8 6 23 1 Enter row 4: 15 7 3 2 9 Enter row 5: 6 14 2 6 0
Row totals: 30 27 40 36 28
Column totals: 34 37 37 32 21
#include int main() { int x[5][5]; int rowTotal[5]; int colTotal[5]={0,0,0,0,0};
for(int i =0;i);
}
Write a program that prints a one-month calendar. The user specified the number of days in the month and
the day of the week on which the month begins:
Enter number of days in month: 31
Enter starting day of the week (1= Sunday, 2=Monday,… 7=Saturday): 3
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Hint: This program is not as hard as it looks. The most important part is a for statement that uses a
variable i to count from l to n, where n is the number of days in the month, printing each value of i.
Inside the loop, an if statement tests whether i is the last day in a week; if so, it prints a new-line
character.
#include int main() { int numDays,dayOfWeek;
printf(“Enter number of days in month: “);
scanf(“%d”,&numDays);
printf(“Enter starting day of the week (1 = Sunday,2=Monday,…7=Saturday): “);
scanf(“%d”,&dayOfWeek);
for(int a=1;a);
}
One of the oldest known encryption techniques is the Caesar cipher, attributed to Julius Caesar. It involves
replacing each letter in a message with another letter that is a fixed number of positions later in the
alphabet. If the replacement would go past the letter Z, the cipher “wraps around” to the beginning of the
alphabet. For example, if each letter is replaced by the letter two positions after it, then Y would be
replaced by A, and Z would be replaced by B. Write a program that encrypts a message using a Caesar
cipher. The user will enter the message to be encrypted and the shift amount (the number of positions by
which letters should be shifted):
Enter message to be encrypted: Go ahead, make my day.
Enter shift amount (1-25): 3
Encrypted message: Jr dkhdg, pdnh pb gdb.
Notice that the program can decrypt a message if the user enters 26 minus the original key:
Enter message to be encrypted: Jr dkhdg, pdnh pb gdb.
Enter shift amount (1-25):23
Encrypted message: Go ahead, make my day.
#include int main() { int sA; /*we can assume message will not exceed 80 characters*/ char message[80];
/*get input*/ printf("Enter message to be encrypted: "); /* DOES NOT WORK IF MESSAGE HAS SPACES. scanf("%s",&message); */
/* [^\n] tells that while the input is not a newline take input */ scanf("%[^\n]%*c",&message);
printf(“Enter shift amount (1-25): “);
scanf(“%d”,&sA);
for(int i=0;i=65 & message[i]=97 & message[i]);
}
Write a program that generates a “random walk” across a 10 x 10 array. The array will contain characters
(all ‘.’ initially). The program must randomly “walk” from element to element, always going up, down, left,
or right by one element. The elements visited by the program will be labeled with the letters A through Z,
in the order visited. Here is an example of the desired output:
Hint: Use the srand and rand functions to generate random numbers. After generating a number, look at
its remainder when divided by 4. There are four possible values for the remainder, 0, 1, 2, and 3, indicating
the direction of the next move. Before performing a move, check that
(a) it will not go outside the array, and
(b) it does not take us to an element that already has a letter assigned.
If either condition is violated, try moving in any of the other directions. If all four directions are blocked,
the program must terminate. Here is an example of premature termination:
Y is blocked on all four sides, so there is no place to put Z.
#include #include #include #include int main(void) { char map[10][10]; bool dirFail[4] = {false,false,false,false}; int dir, posX=0, posY=0; bool moveMade = false;
/* Seeds the pointer for rand() so that the number pulled from the long integer list will not be identical each time the program runs */ srand(time(NULL));
/* initialize the map with all '.' characters */ for (int i =0;i 9) dirFail[1]=true;
/* down */ if(map[posX+1][posY]!='.' || posX+1 >9) dirFail[2]=true;
/* left */ if(map[posX][posY-1]!='.' || posY-1); }