Document1 Wrd File Insert Design Lxext References Mailings Review VirHrp Tell me
ID: 3702073 • Letter: D
Question
Document1 Wrd File Insert Design Lxext References Mailings Review VirHrp Tell me what you want to do Home Cut Copy Format Painter IU- Sharee Find Replace Faste Blu-ab x,x'A-3?,A- =?,A-EL, 1Norma. No Spac Heading 1 Heading 2 Title Subtitle Subte Em Emphassitense E Strong Quote ?b Select Paragrap Ecting This HW is a practice on using pointers. Use pointer notation instead of common subscript array notation (i.e. *(xti) instead x]) Your program should A- At startup: generate integer random numbers and place them in a file. Each number on a single line The numbers should range between 1-200 and the program should generate between 100-150 numbers. This means each time the program is executed, a file is created containing between 100-150 numbers with values between 1-200. Make sure you close the file once the numbers are generated. This should be done in a separate function B- The program should then read the file and create a dynamic array that holds the numbers. The size of the array is the number of elements in the file just read. This should be in a separate function C- Print values of the array: this should be done by creating a function that accepts two arguments, a const integer pointer to the array and size printValues const int "arr, int size) D- The program should ask the user to enter a "key value" and then find all numbers that are above that key value, store them in a dynamic array and return that array. Then your program should call the printValues function passing the new array. This should be implemented in as a pointer returning from a function (i.e. the function should return a pointer to the new array) 4- Print the array in reverse. Your program (via a function) should create a new copy of the array, except that the elements should be in reverse. The function should return a pointer to the new array. Then call printValues to show the values of the new array Make sure you do not forget to deallocate memory after use Pae 1 uf1 2 words +137 O Type here to search 1:03 I'M 45/2018Explanation / Answer
Solution:
code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void generateRandomNums()
{
srand(time(NULL));
FILE *fp;
fp = fopen("numbers.txt", "w");
if (fp == NULL)
{
printf("Unable to open file ");
exit(1);
}
int i, n = 100 + (rand() % 50);
while (i < n)
{
fprintf(fp, "%d ", 1 + (rand() % 200));
i++;
}
fclose(fp);
}
int *readNumbers(int *size)
{
FILE *fp;
fp = fopen("numbers.txt", "r");
if (fp == NULL)
{
printf("Unable to open file ");
exit(1);
}
int n = 0, k;
int *x = (int *)malloc(sizeof(int));
while (fscanf(fp, "%d", (x + n)) != -1)
{
n++;
x = (int *)realloc(x, (n + 1) * sizeof(int));
}
*size = n;
return x;
}
void printValues(const int *arr, int n)
{
int i = 0;
while (i < n)
{
printf("%d ", *(arr + i));
i++;
}
printf(" ");
}
int *aboveNums(int key, int *x, int n, int *aboveSize)
{
int i = 0, k = 0;
int *above = (int *)malloc(sizeof(int));
while (i < n)
{
if (*(x + i) > key)
{
*(above + k) = x[i];
k++;
above = (int *)realloc(above, (k + 1) * sizeof(int));
}
i++;
}
*aboveSize = k;
return above;
}
int *reverse(int *arr, int n){
int i=0;
int *n_arr = (int *)malloc(n*sizeof(int));
while(i<n){
*(n_arr+i) = *(arr+n-i-1);
i++;
}
return n_arr;
}
int main()
{
generateRandomNums();
int n, key, aboveSize;
int *x = readNumbers(&n);
printf("The values from file are(%d): ", n);
printValues(x, n);
printf(" Enter key value: ");
scanf("%d", &key);
int *above = aboveNums(key, x, n, &aboveSize);
printf("The values above %d are(%d): ", key, aboveSize);
printValues(above, aboveSize);
int *rev = reverse(above, aboveSize);
printf(" The reverse of above numbers are: ", key, aboveSize);
printValues(rev, aboveSize);
printf(" ");
free(x);
free(above);
free(rev);
return 0;
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.