1. Pointers as function arguments
Suppose you are given an array of integers and an integer value key. Write a program to calculate the indices of the two numbers in the array such that they add up to key. You can assume that there is exactly one solution for each input array and key.
Example input/output#1:
Enter the length of the array: 7
Enter the elements of the array: 3 5 1 4 9 8 11
Enter the key value: 19
Output: 56
Example input/output#2:
Enter the length of the array: 4
Enter the elements of the array: 2 5 8 4
Enter the key value: 10
Output: 0 2
The program should include the following function. Do not modify the function prototype.
void find_indices(int a[], int n, int key, int * index1, int *index2);
arepresents the input array with length n, and key represents the key value. The index1 and index2 parameters point to variables in which the function will store the indices of the array elements that add up to key.
1)Name your program indices.c
2)In the main function, ask the user to enter the length of the input array, declare the input array. Then ask the user to enter the elements of the array.
3)In the main function, call the find_indicesfunction to find the indices of the two array elements that add up to key.
4)The main function displays the two indices.
2. Suppose you are given an array of n integers. Assume each integer in the array appear either one or twice in the array. Display all the integers that appear twice.
Example input/output #1:
Enter the length of the array: 10
Enter the elements of the array: 3 5 1 4 4 3 9 1 8 11
Output: 3 1 4
Exampleinput/output #2:
Enter the length of the array: 8
Enter the elements of the array: 5013 4 1 7 3 5
Output: 5
1)Name your program arrays.c
2)Include the following function to find elements that appear twice in the array:
voidfind_duplicates(int *a, int n, int *b, int *size);
a represents the input array with length n, and b represents the output array. The size parameter points to a variable in which the function will store the number of integers that appear twice (the actual length of array b).
This function should use pointer arithmetic–not subscripting –to visit array elements. In other words, eliminate the loop index variables and all use of the [] operator in the function.
3)In the main function, ask the user to enter the length of the input array, declare the input array. Then ask the user to enter the elements of the array, and declare the output array with half of the length of the input array.
4)In the main function, call the find_duplicatesfunctionto find elements that appear twice and store the elements in array b.
5)In the main function, display the output array. 6)Pointer arithmetic in NOT required in the main function


0 comments