Sample Homework Assignment #8 [0 pts]


Overview

This is a sample assignment in which you implement various functions.

Solutions

Solutions are available in this archive


Exercise #1: Implement the getInputArray function [0 pts]

The first thing program hw8_main_program does is call a getInputArray function. The C prototype of this function is as follows:

char *getInputArray(int *num_elements);

You must implement this function in hw8_functions.asm (which contains an empty placeholder for it). Function getInputArray does:

To implement this function you need to allocate memory. To this end you should call the allocate_memory function, which is provided in hw8_functions.asm. This function takes a number of bytes as argument and allocates an contiguous zone of this many bytes. Its return value is the address of the first bytes in that allocated zone.

Here are example interactions with the program for various input:

% ./hw8_main_program
5
12
43
23
1
6
The 5 entered values are: 12 43 23 1 6 
findFirstIndex not implemented!

% ./hw8_main_program
8
1
2
3
4
5
4
3
2
The 8 entered values are: 1 2 3 4 5 4 3 2 
findFirstIndex not implemented!

Exercise #2: Implement the findFirstIndex function [0 pts]

After calling getInputArray, the program in hw8_main_program.asm calls a function called findFirstIndex, that you must implement. The C prototype for this function is:

int findFirstIndex(char *array, int num_array_elements, int (*fun)(int));

The 3rd argument above is a pointer to a function that takes a 4-byte integer as argument and returns a 4-byte integer, which has only value 0 or 1.

The findFirstIndex function does:

The main program uses findFirstIndex to find the index of the first element that’s a power of 2, and the index of the first element that’s a multiple of 7. Here are example interactions with the program:

% ./hw8_main_program
5
17
23 
32
5
8
The 5 entered values are: 17 23 32 5 8 
The index of the 1st power of two element is: 2
The index of the 1st multiple of 7 element is: -1
findAllIndices not implemented!

% ./hw8_main_progra
10
98
96
49
53
128
5
4
3
2
56
The 10 entered values are: 98 96 49 53 128 5 4 3 2 56 
The index of the 1st power of two element is: 4
The index of the 1st multiple of 7 element is: 0
findAllIndices not implemented!

Exercise #3: Implement the findAllIndices function [0 pts]

After calling findFirstIndex, the main program calls another function findAllIndices, which you must implement. The C prototype of this function is:

int *findAllIndices(char *array, int num_array_element, int (*fun)(int), int *found_count);

The findAllIndices function does:

Sample interactions of the program are as follows:

% ./hw8_main_program
7
3
4
6
16
7
9
32
The 7 entered values are: 3 4 6 16 7 9 32 
The index of the 1st power of two element is: 1
The index of the 1st multiple of 7 element is: 4
3 of the elements are powers of 2
The powers of 2 are at indices: 1 3 6 
1 of the elements are multiples of 7
The multiples of 7 are at indices: 4 

% ./hw8_main_program
10
14
21
28
31
55
49
9 
111
42
5
The 10 entered values are: 14 21 28 31 55 49 9 111 42 5 
The index of the 1st power of two element is: -1
The index of the 1st multiple of 7 element is: 0
0 of the elements are powers of 2
5 of the elements are multiples of 7
The multiples of 7 are at indices: 0 1 2 5 8