Directions: Complete the following lab assignment using the description given in
ID: 3821071 • Letter: D
Question
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 notation/arithmetic 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 the input1.txt and input2.txt from blackboard.
Implement the following functions for the lab assignment.
int loadNumbers(char*, int *,int): 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 printNumbers(int*, 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 main(): Use command line arguments to read the file names and the length of records. Call loadNumbers function and load the inputs from the file into two integer pointers for which you need to malloc space. Print the numbers and then perform all the stats and print those values too.Then call the writeData function. See sample output below and the sample output.txt on blackboard and follow that.
Remember to free all the malloc-d memory correctly.
Note
Use of arrays is not allowed for the lab assignment instead start with an integer pointer and use malloc function to allocate only the required amount of memory.
Use POINTER NOTATION AND POINTER ARITHMETIC only to implement the assignment.
Sample Output
dpstm3:~$ ./a.out
USAGE ERROR : CHECK THE COMMAND LINE ARGUMENTS
dpstm3:~$ ./a.out input1.txt
USAGE ERROR : CHECK THE COMMAND LINE ARGUMENTS
dpstm3:~$ ./a.out 8 input1.txt input2.txt
USAGE ERROR : CHECK THE COMMAND LINE ARGUMENTS
dpstm3:~$ ./a.out 8 input1.txt input2.txt output1.txt output2.txt
The numbers are:
1 11 12 13 14 7 8 9
The mean of all the numbers = 9.38
The minimum of all the numbers = 1
The maximum of all the numbers = 14
Output written to the output file --->output1.txt
The numbers are:
9 8 7 6 5 4 3 2
The mean of all the numbers = 5.50
The minimum of all the numbers = 2
The maximum of all the numbers = 9
Output written to the output file --->output2.txt
********** BONUS **********
The numbers reversed from input1.txt file are:
9 8 7 14 13 12 11 1
The numbers reversed from input2.txt file are:
2 3 4 5 6 7 8 9
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){
if(argc<3){
printf(" Invalid Command line argument. Uses : >Program_name input_file output_file ");
return 1;
}
int* arr;
int size = 4;
arr = (int*)malloc(size*sizeof(int));
if(loadNumbers(argv[1],arr,size)==0){
printf("Error in opning file");
}
printf("The numbers are : ");
printNumbers(arr,size);
printf(" The mean of all the numbers = %f",findAverage(arr,size));
printf(" The minimum of all the numbers = %d",findMax(arr,size));
printf(" The maximum of all the numbers = %d",findMin(arr,size));
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.