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

Code in C only, only use basic cs1050 coding because the compiler is very strick

ID: 3820803 • Letter: C

Question

Code in C only, only use basic cs1050 coding because the compiler is very strick. Dont use anything fancy, plain and simple, also make sure you do not use arrays. Start with an integer pointer and use malloc to allocate only the required anount of memory USE POINTER NOTATION AND POINTER ARITHMETIC only to implement the assignment. This is output1.txt This is output2.txt Directions: Complete the following lab assignment using the description given in each section. Purpose: 1. Use command line arguments to do the assignment. 2. Use pointers and pointer notationarithmetic in implementation only. 3. Use functions from relevant libraries for the lab. Description The lab 11 is on malloc, command line and file processing. Download theinputi.txt and input2.txt from blackboard. Implement the following functions for the lab assignment. int loadNumbers (char int ,in): This function takes the input file name, the integer pointer to the array and the count of numbers. It opens the input file, if unable to open it returns 0 otherwise it loads the numbers from the file to the integer pointer and returns 1 at the end. void printNumbersoint ,int): This function prints the content of the array on to the screen, takes in the integer pointer and the size of the array float findAverage(int int): Takes in the integer pointer and find the average of all the numbers and returns it. int findMax(int ,int): Takes in the integer pointer and the size and finds the maximum number in the array. int findMin(int int): Takes in the integer pointer and the size and finds the maximum number in the array. int writeData(char *,int ,int): Takes the output filename, integer pointer denoting the integers, and the size of the array. Open the output file in this function, write all the information which is on the screen also to the output file and return 1 at the end. If unable to open return 0 and exit out of the code. Call this function twice, once for each input file and each set of values. Refer the sample output.txt posted on blackboard. int main0: Use command line arguments to read the file names and the length of records. Call

Explanation / Answer

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int loadNumbers(char*, int*, int);
void printNumbers(int*, int);
float findAverage(int* ,int);
int findMax(int* ,int);
int findMin(int* ,int);
int writeData(char*,int*,int);

int main(int argc, char**argv){
   int* arr;
   int size = 4;
   arr = (int*)malloc(size*sizeof(int));
  
   if(loadNumbers(argv[1],arr,size)==0){
       printf("Error");
   }
   printNumbers(arr,size);
   if(writeData(argv[2],arr,size)==1){
       printf("Output Written in 'output.txt' file");
   }
}

int loadNumbers(char* filename, int* arr, int size){
   FILE* file = fopen (filename, "r");
   if(file == NULL)
       return 0;

   int count = 0;
   while (!feof (file))
   {
       int num;
       fscanf (file, "%d", &num);
       //printf(" %d ",num);
       *arr = num;
       arr++;
       count++;
       if(count>=size)
       break;
   }
   return 1;
}

void printNumbers(int* arr, int size){
int i;
   int* temp = arr;
   for(i=0; i<size; i++){
   printf("[%d] ",*temp);
   temp++;
   }
   printf(" ");
}

float findAverage(int* arr ,int size){
float avg = 0;
int i;
   int* temp = arr;
   for(i=0; i<size; i++){
   avg += (float)*temp;
   temp++;
   }
   return (avg/(float)size);
}

int findMax(int* arr ,int size){
int max = -100;
int i;
   int* temp = arr;
   for(i=0; i<size; i++){
   if(*temp > max)
   max= *temp;
   temp++;
   }
   return max;
}

int findMin(int* arr ,int size){
int min = 10000;
int i;
   int* temp = arr;
   for(i=0; i<size; i++){
   if(*temp < min)
   min = *temp;
   temp++;
   }
   return min;
}

int writeData(char* filename, int* arr, int size){
FILE* file = fopen (filename, "w");
   if(file == NULL)
       return 0;

   int count = 0;
   fprintf(file,"The numbers are : ");
   int* temp = arr;
   while (count<size)
   {
       int num;
       num =*temp;
       //fscanf (file, "%d", &num);
       fprintf(file," %d ",num);
       //printf(" %d ",num);
       temp++;
       count++;
       if(count>=size)
       break;
   }
   fprintf(file," The min is: %d ",findMin(arr,size));
   fprintf(file," The max is: %d ",findMax(arr,size));
   fprintf(file," The average is: %f ",findAverage(arr,size));
  
   return 1;
}

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