Lab 3
Participation during lab earns a lab mark.
Submission of work (functional or not) can contribute towards a bonus mark,
any work resembling AI will not be considered.
Objective:
• Arrays, pointers, and multi-dimensional arrays
Learning outcomes:
• The acquisition, application and integration of knowledge
• Research skills, including the ability to define problems and access,
retrieve and evaluate information (information literacy)
• Literacy and numeracy skills
• Interpersonal and communications skills
Example Problems and Solutions
Example 1: Create an array with 10 elements. Save the numbers 1 through 10
into the array using array notation. Then use pointer notation to save the
numbers in descending order.
#include<stdio.h>
int main() {
int arr[10];
//Save 1-10 in array using array notation
for(int k = 1; k <= 10; k++)
arr[k-1] = k;
//Save 10-1 in array using pointer notation
for(int k = 0; k < 10; k++)
*(arr+k) = 11 - *(arr+k); //or just 10-k
//print the result using pointer notation
int *p = arr;
for(int k = 0; k < 10; k++, p++)
printf("%d ", *p);
}
© 2024 Ryan Bluteau. All rights reserved. 1
Example 2: Create a 5x10 array and pass it to a function. The function will
compute the sum of all elements in the 2D array.
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
#define M 10
float total_sum(float arr[N][M]){
float sum = 0.0;
//pointer notation
for(int k = 0; k < N; k++)
for(int j = 0; j < M; j++)
sum += *(*(arr+k)+j);
return sum;
}
int main() {
float arr[N][M] = {}; //default all elements to 0.0
srand(time(0));
//array notation
for(int k = 0; k < N; k++)
for(int j = 0; j < M; j++)
arr[k][j] = rand();
printf("Total sum is: %f\n", total_sum(arr));
return 0;
}
© 2024 Ryan Bluteau. All rights reserved. 2
Example 3: Make a function to print an array without defining an integer
variable (int pointer is fine). The array length is fixed to length 10 for the whole
program.
#include<stdio.h>
#define N 10
void print_array(int *array){
int *p = array;
while(p < array + N){
printf("%d ", *p);
p++;
}
return;
}
int main() {
//7 elements (last 3 are 0)
int array[N] = {2, 4, 6, 8, 10, 12, 14};
print_array(array);
return 0;
}
© 2024 Ryan Bluteau. All rights reserved. 3
Practice Problems
Problem A: Make a function “copy_by_array” that copies one array to another
of the same length using array notation. Randomly populate the first array, copy
it to a second, then print the second array in main. Make a second function
“copy_by_pointer” which uses pointer notation to do the same task.
Problem B: Create an NxM array. Make a function to transpose it. The function
accepts an NxM array and an MxN array where the function transposes the first
array and saves it into the second.
Problem C: Create an array of characters where their values are provided by
the user. Make a function to corrupt the array by randomly changing a-z / A-Z
characters (while still being in the range of a-z and A-Z) in the array. The chance
of corruption is provided by the user.
Problem D: Given the code below, use pointer notation to determine what
position in the array the pointer is located. Hint: If we are at position 155, we
can /100 to get x and %100 to get y. Example: 155/100 = 1, 155%100 = 55. So
we are at (1, 55).
#include<stdio.h>
int main() {
int array[50][100] = {}; //all elements are 0
int *position = &array[rand()%50][rand()%100];
int x = _____________;
int y = _____________;
printf("The position found is: (%d, %d)\n", x, y);
return 0;
}
© 2024 Ryan Bluteau. All rights reserved. 4
Problem E: Given the code below, what are the values stored in A to G (without
running the code and printing them).
#include<stdio.h>
int A[5] = {1, 5, 9};
int C[2];
int F[3][2] = {1, 2, 3, 4, 5, 6};
int G[6] = {1, 2, 3, 4, 5, 6};
int main() {
int B[3] = {};
int D[5] = {C[1], 2};
int E = D[C[0] + A[4]];
}
Problem F: Create a 1D array of length 100. Make a function, getter, that
receives the array and a 2D index (k, j). The function should return the element
at position (k, j) as if the array was a 20x5 matrix. Make another function to set
values as well. Randomly populate the array using the setter function, and print
the array using the getter function.
© 2024 Ryan Bluteau. All rights reserved. 5