0% found this document useful (0 votes)
4 views14 pages

W9 - Practice

This document provides a practice guide for working with arrays and indexes in C programming. It includes exercises on defining and initializing arrays, accessing elements, computing metrics, and debugging techniques. The document is structured into analysis, manipulation, and creation sections to enhance understanding and application of array concepts.

Uploaded by

phanhacambodia22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

W9 - Practice

This document provides a practice guide for working with arrays and indexes in C programming. It includes exercises on defining and initializing arrays, accessing elements, computing metrics, and debugging techniques. The document is structured into analysis, manipulation, and creation sections to enhance understanding and application of array concepts.

Uploaded by

phanhacambodia22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

W9 - PRACTICE

Arrays and Indexes

At the end of this practice, you should be able to…


 Define and initialize a fixed-size array in C
 Access elements of an array using indexing
 Apply loops to iterate through array elements for various operations

 Compute Array Metrics:


- minimum and maximum values
- -first occurrence of a given value like 7
- number of occurrences

 Use debugging techniques to check for errors

How do we structure exercises?


We organize each practice into 3 parts:

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

Are you lost?


You can read the following documentation to be ready for this practice

https://www.w3schools.com/c/c_arrays.php
ANALYSE

EX 1 (Bug Tracking)

Look at the bellow code.

Q1 - Color in red the loops, in blue the variables, in green the conditions
int arr[5];

for (int index = 1; index <= 5; index ++) {


arr[index] = index * 2;
}

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.

STEP index ARR


Before Loop
1 ? { ?, ?, ?, ?, ? }
During Loop
2 1 { ?, 2, ?, ?, ? }
3 2 { ?, 2, 4, ?, ? }
4 3 { ?, 2, 4, 6, ? }
5 4 { ?, 2, 4, 6, 8 }
6 5 { ?, 2, 4, 6, 8 }

Q3 – Identify the BUG in this code and explain why it causes problems.
The bug is when they initialize index equal 1

Q4 – Propose a correction to fix this bug.


thus it should be equal to 0.
EX 2 (For Loop)

Look at the bellow code.

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)

Look at the bellow code.

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++;
}

Q2 - Can we convert this code to use a for loop?

Warning: we don’t NOT allow to use A BREAK inside a loop.

If NO, explain why

If YES, complete the bellow table to define your FOR loop:


Initialization bloc int i = 0
Break condition i<5
Increment i++
EX 4 (While to For Loop)

Look at the bellow code.

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.

Specify the updated FOR loop:


Initialization bloc int I = 2
Break condition I <5
Increment i++

Write down the updated code:


int arr[5] = {10, 20, 30, 40, 50};
for (int i = 2; i < 5; i++) {
printf("%d ", arr[i]);
}
EX 5 (Code comparison)

Look at the bellow 2 codes.

Q1 - Color in red the loops, in blue the variables, in green the conditions

CODE A
int arr[5] = {1, 2, 3, 4, 5};

for (int i = 0; i < 5; i++) {


arr[i] *= 2;
}
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}

CODE B
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i] * 2);
}

Q2 – Will those 2 codes produce the same output?

Explain why
yes because since code B is a shortcut of code A, so both of the codes have the same output .

Q3 – Which code is better regarding the objective of this program?

Explain why
definitely code B because it is easier to understand like code B than code A.
MANIPULATE

EX 1 (Print array content)

Write a program to display each array numbers, followed by -


INPUT Array of 5 integers

OUTPUT The array content, separated by ‘-’

Examples
INPUT OUTPUT
{10, 9, 8, 7, 1} 10-9-8-7-1

{1, 1, 1, 1, 1,1} 1-1-1-1-1-1

Q1 – Fill up the gaps on bellow code:

int arr[5] = {1, 2, 3, 4, 5};


for (int i = 0; i<5; i++ ) {
printf(“%d”, arr[i]);

if (i <4) {
printf("-"); // We print - if we are not at the last index
}
}

Q2 – Test your code works well with the above examples.

Q3 – Convert your code to use a WHILE instead of a LOOP


// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#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

INPUT Array of 5 integers

OUTPUT The array content, separated by ‘-’

Examples
INPUT OUTPUT
{8, 9, 15, 7, 2} 3

{7, 9, 15, 7, 2} 0

{8, 9, 15, 9, 2} -1

Q1 – Write the PSEUDO CODE to solve this problem

You can use the following blocs:


INPUT <variable>
SET <variable> TO <value>
FOR <number> FROM <start> TO <end>
REPEAT <value times>
WHILE <condition>
IF <condition>
PRINT
SET <found> TO <0>
SET <i> TO <0>
PRINT <Enter 5 numbers>
FOR <i> FROM <0> TO <5>
INPUT <arr[ ]>
FOR <i> FROM <0> TO <5>
IF <arr[i] ==7>
PRINT <arr[i]>
SET <found> TO <1>
SET <i> TO <i+1>
IF <!found>
PRINT <-1>
PRINT <0>

Q2 – After your pseudo code if validated, convert it to C code and test it


#include <stdio.h>

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]);
}

for (i = 0; i < 5; i++) {


if (arr[i] == 7) {
found = i;
i = 5;
}

}
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

INPUT Array of 5 integers

OUTPUT The array content, separated by ‘-’

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 .

Q2 – Convert the previous code to fit with this new problem.


#include <stdio.h>
int main() {
int arr[5];
int i;
int found = -1;

printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}

for (i = 0; i < 5; i++) {


if (arr[i] == 7) {
found = i;
}
}
printf("%d\n", found);
return 0;
}
EX 4 (Find the minimum score)

Write a program to find the minimum of 5 elements in an array.

INPUT Array of 5 integers

OUTPUT The minimum number

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

Q1 – We want to solve this problem with the bellow pseudo code

- Execute this code mentally with the above examples


- Assert the output is equal to the expected result

SET min TO numbers[0]


FOR i IN RANGE (1 ..5)
If numbers[i]<min
SET min TO number[i]

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

Q3 – Finally code this algorithm in C.


#include <stdio.h>
int main() {
int numbers[5];
int min;
printf("Enter 5 numbers: ");
for (int i =0; i < 5; i++) {
scanf("%d", &numbers[i]);

}
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;

EX 5 (Find the maximum score)

Write a program to find the maximum of 5 elements in an array.

INPUT Array of 5 integers

OUTPUT The maximum number

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?

Change this pseudo code, write in red your changes:


SET min TO numbers[0]
FOR i IN RANGE (1 ..5)
If numbers[i]<min
SET min TO number[i]

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;

You might also like