Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Introduction This lab will give you practice using pointers and dynamic memory a

ID: 3806394 • Letter: I

Question

Introduction This lab will give you practice using pointers and dynamic memory allocat tion in C. You will write a program that inputs an array of ints, prints out the array in the order that the values were entered, reverses the array and finally prints out the values of the array after the reversal. Getting started: 1. Name your file reverse C 2. Use the following code to get started: #include "stdio.h" #include "stdlib.h" needed for malloc void swap void reverse (int *in-arr, int in-num) int main() int how many; printf ("How many numbers are you going to input? return 0; 3. At the command line, compile reverse.c by typing gCC reverse. C You should receive no compiler errors or warnings.

Explanation / Answer

#include <stdio.h>
#include <malloc.h>
void swap(int *in_arr, int a, int b);
void reverse(int *in_arr, int in_num);
int main()
{
int n,i;
int *arrayIn;
printf("How many numbers are you going to input? ");
scanf("%d", &n);
arrayIn = (int*) malloc(n * sizeof(int));
for(i=0; i<n; i++){
scanf("%d", &arrayIn[i]);
}
printf("Here is the array: ");
for(i=0; i<n; i++){
printf("%d ", arrayIn[i]);
  
}
printf(" ");
reverse(arrayIn,n);
printf("Here is the array reversed: ");
for(i=0; i<n; i++){
printf("%d ", arrayIn[i]);
  
}
printf(" ");

return 0;
}
void reverse(int *in_arr, int in_num) {
int i;
for(i=0; i<in_num/2; i++){
swap(in_arr, i, in_num-1-i);
}
}

void swap(int *in_arr, int a, int b) {
int t;
t= in_arr[a];
in_arr[a] = in_arr[b];
in_arr[b] =t;
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                                           

How many numbers are you going to input? 4                                                                                                                                                                                                                             

3 14 1 5                                                                                                                                                                                                                                                               

Here is the array:                                                                                                                                                                                                                                                     

3 14 1 5                                                                                                                                                                                                                                                               

Here is the  array reversed:                                                                                                                                                                                                                                           

5 1 14 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote