Lab 3
Lab 3
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
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
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();
int main() {
//7 elements (last 3 are 0)
int array[N] = {2, 4, 6, 8, 10, 12, 14};
print_array(array);
return 0;
}
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 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;
}
#include<stdio.h>