Rewrite the program to use pointers. Do not change the declarations of any funct
ID: 3556150 • Letter: R
Question
Rewrite the program to use pointers. Do not change the declarations of any functions; only convert their definitions to use pointers to array elements rather than array element indexing. (In other words, make all the brackets associated with array element access disappear.) Rewrite all loops to use pointer variables rather than integer counters.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
/* Asks the user to enter a series of up to 'size' nonnegative integers, reads them from the terminal and stores in the 'number' array. Returns the number of integers read and placed into the array. */
int read_array(int number[], int size);
/* Sorts the 'number' array of 'size' integers, using the selection sort algorithm. The selection sort algorithm, when given an array with n elements, does the following: 1. Searches the array to find the largest element, then moves it to the last position in the array. 2. Calls itself recursively to sort the first $n-1$ elements of the array. */
void selection_sort(int number[], int size);
/* Prints the 'message', followed by 'size' integers from the 'number' array, and a new line. */
void print_array(char message[], int number[], int size);
/* If 'condition' is false (0), prints the 'message' and exits the program with an error code. */
void ensure(bool condition, char message[]);
int main() {
int number[MAX_SIZE];
int size;
size = read_array(number, MAX_SIZE);
ensure(size > 0, "The array is empty ");
print_array("Initial array: ", number, size);
selection_sort(number, size);
print_array("Sorted array: ", number, size);
return EXIT_SUCCESS;
}
int read_array(int number[], int size) {
int i = 0, temp;
while(1)
{
printf(" Enter %d Integer :", (i + 1));
scanf("%d",&temp);
if(temp < 0)
break;
if(i == size){
printf(" maximum number of integers has been entered");
break;
}
number[i++] = temp;
}
printf(" ");
return i;
}
void selection_sort(int number[], int size) {
if(size == 1)
return;
int max = 0;
int i = 0;
for(i = 1; i < size; i++)
if(number[i] > number[max])
max = i;
int temp = number[size - 1];
number[size - 1] = number[max];
number[max] = temp;
//print_array(" temp:",number,size);
selection_sort(number,size - 1);
}
void print_array(char message[], int number[], int size) {
int i;
printf("%s", message);
for(i = 0; i < size; i++)
printf("%d ", number[i]);
printf(" ");
}
void ensure(bool condition, char message[]) {
if (!condition) {
printf("%s",message);
exit(EXIT_FAILURE);
}
}
Explanation / Answer
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
/* Asks the user to enter a series of up to 'size' nonnegative integers, reads them from the terminal and stores in the 'number' array. Returns the number of integers read and placed into the array. */
int read_array(int *number, int size);
/* Sorts the 'number' array of 'size' integers, using the selection sort algorithm. The selection sort algorithm, when given an array with n elements, does the following: 1. Searches the array to find the largest element, then moves it to the last position in the array. 2. Calls itself recursively to sort the first $n-1$ elements of the array. */
void selection_sort(int *number, int size);
/* Prints the 'message', followed by 'size' integers from the 'number' array, and a new line. */
void print_array(char *message, int *number, int size);
/* If 'condition' is false (0), prints the 'message' and exits the program with an error code. */
void ensure(bool condition, char *message);
int main() {
int *number=(int *)malloc(sizeof(int)*MAX_SIZE);
int size;
size = read_array(number, MAX_SIZE);
ensure(size > 0, "The array is empty ");
print_array("Initial array: ", number, size);
selection_sort(number, size);
print_array("Sorted array: ", number, size);
free(number);
return EXIT_SUCCESS;
}
int read_array(int *number, int size) {
int i = 0, temp;
while(1)
{
printf(" Enter %d Integer :", (i + 1));
scanf("%d",&temp);
if(temp < 0)
break;
if(i == size){
printf(" maximum number of integers has been entered");
break;
}
number[i++] = temp;
}
printf(" ");
return i;
}
void selection_sort(int *number, int size) {
if(size == 1)
return;
int max = 0;
int i = 0;
for(i = 1; i < size; i++)
if(number[i] > number[max])
max = i;
int temp = number[size - 1];
number[size - 1] = number[max];
number[max] = temp;
//print_array(" temp:",number,size);
selection_sort(number,size - 1);
}
void print_array(char *message, int *number, int size) {
int i;
printf("%s", message);
for(i = 0; i < size; i++)
printf("%d ", number[i]);
printf(" ");
}
void ensure(bool condition, char *message) {
if (!condition) {
printf("%s",message);
exit(EXIT_FAILURE);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.