W9 - Practice
W9 - Practice
ANALYSE Understand existing codes, find the bugs or complete missing gaps
MANIPULATE Ensure you can apply the theory with some basic challenges
CREATE Express your creativity with more complex challenges
https://www.w3schools.com/c/c_arrays.php
ANALYSE
EX 1 (Bug Tracking)
Q1 - Color in red the loops, in blue the variables, in green the conditions
int arr[5];
Q2 - Complete the bellow table, by specifying the variables state during every step of the execution
- Note: We use ? to represent a variable not initialized yet.
Q3 – Identify the BUG in this code and explain why it causes problems.
The bug is when they initialize index equal 1
Q1 - Color in red the loops, in blue the variables, in green the conditions
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i += 2) {
printf("%d ", arr[i]);
}
Q2 - Complete the below table, by specifying the index value and the console prints during every step of
the execution
STEP i CONSOLE
Before Loop
1 ? 0
During Loop
2 0 10
3 2 30
4 4 50
5 6 50
6
EX 3 (While to For Loop)
Q1 - Color in red the loops, in blue the variables, in green the conditions
int arr[5] = {1, 2, 3, 4, 5};
int i = 0;
while (i < 5) {
printf("%d ", arr[i]);
i++;
}
Q1 - Color in red the loops, in blue the variables, in green the conditions
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
Q2 - Modify the loop to display only the last three elements of the array.
Q1 - Color in red the loops, in blue the variables, in green the conditions
CODE A
int arr[5] = {1, 2, 3, 4, 5};
CODE B
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i] * 2);
}
Explain why
yes because since code B is a shortcut of code A, so both of the codes have the same output .
Explain why
definitely code B because it is easier to understand like code B than code A.
MANIPULATE
Examples
INPUT OUTPUT
{10, 9, 8, 7, 1} 10-9-8-7-1
if (i <4) {
printf("-"); // We print - if we are not at the last index
}
}
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int i=0;
while(i<5) {
printf("%d", arr[i]);
if (i <4) {
printf("-"); // We print - if we are not at the last index
}
i++;
}
return 0;
}
EX 2 (First index of 7)
Write a program to find the index of the FIRST 7 in the array of 5 elements - Or -1 if no 7 in the array
Examples
INPUT OUTPUT
{8, 9, 15, 7, 2} 3
{7, 9, 15, 7, 2} 0
{8, 9, 15, 9, 2} -1
int main() {
int arr[5];
int i;
int found =0;
printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
}
printf("first array index: %d\n", found);
return 0;
}EX 3 (Last index of 7)
Write a program to find the index of the LAST 7 in the array of 5 elements - Or -1 if no 7 in the array
Examples
INPUT OUTPUT
{8, 9, 15, 7, 2} 3
{7, 9, 15, 7, 2} 3
{8, 9, 15, 9, 2} -1
Q1 – Compared to the last exercise, what would you change to adapt to this new situation?
Explain here you changes (loop break condition, stored variables, if conditions etc.…
loop break of first index of 7 is when it meet its condition it will end the loop, but loop break of
last index of 7 is different, meaning it check until the end the loop .
printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
Examples
INPUT OUTPUT
{8, 9, 15, 7, 2} 2
{10, 9, 8, 7, 6, 5} 5
{1, 2, 3, 4, 5} 1
{1, 1, 1, 1, 1} 1
PRINT min
Q2 – Describe the variables you need for this problem, their types and goal.
VARIABLE TYPE GOAL
min int to store first element of array elements
numbers array to store all the array elements
i int to give the condition for loop
}
min = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] < min) {
min = numbers[i];
}
}
printf("The minimum number is: %d\n", min);
return 0;
Examples
INPUT OUTPUT
{8, 9, 15, 7, 2} 15
{10, 9, 8, 7, 6, 5} 10
{1, 2, 3, 4, 5} 5
{1, 1, 1, 1, 1} 1
Q1 – What will need to adapt from last code (minimum) to fit to this new problem?
PRINT min
Q2 – Finally code this algorithm in C.
#include <stdio.h>
int main() {
int numbers[5];
int max;
printf("Enter 5 numbers: ");
for (int i =0; i < 5; i++) {
scanf("%d", &numbers[i]);
}
max = numbers[0];
for (int i = 1; i < 5; i++) {
if (numbers[i] > max) {
max = numbers[i];
}
}
printf("The minimum number is: %d\n", max);
return 0;