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

(THIS PROGRAM IS IN C NOT C++) Pointers, a rray s and FILES problem: Write a pro

ID: 3713961 • Letter: #

Question

(THIS PROGRAM IS IN C NOT C++)

Pointers, a

rray

s and FILES

problem:

Write a program with four user functions. Create

a FILE pointer. Use fopen() to

create a file called “AGE.txt.” Create an integer array

(ageArray) with 99 elements

-

initialized it to (

-

99 ). Create a pointer to the first array

element (start pointer) and a pointer to the last element (end pointer). Call function four.

Call function

one. Call function three. Call function two. Call function four.

Function one:

Pass the FILE pointer and array SIZE to the function. The function will return nothing.

The function will fill the file “AGE.txt” with (SIZE) random integer numbers between 0 and

99

inclusive.

Function two:

Pass the FILE pointer, array startPTR and a

rray endPtr to the function. The function will

return nothing. The function will read the integers from the “AGE.txt” file and write them

into the ageArray using only pointers.

Function three:

Pass the FILE pointer to the function. The function will re

turn nothing. The function will

print the contents of the file “AGE.txt”

10 numbers per line.

Function four:

Pass the

array startPTR and array endPtr to the function

.

The function will return nothing.

Print the array passed to the function to the scre

en 10 values per line using only pointers.

Explanation / Answer

Below is the c programme given for above description :

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

FILE *fp;


void function_one(FILE *fp,int n)
{
for(int i=0;i<n;i++)
{
    int r = rand()%100;
    fprintf(fp, "%d ",r);
}
fclose(fp);
}

void function_two(FILE *fp,int *start,int *end)
{
fp = fopen("AGE.txt","r");
int *start1 = start;
while(!feof(fp))
{
    int r;
    fscanf(fp,"%d ",&r);
    printf("%d ",r);
    *start1 = r;
    start1+=1;
}
}

void function_three(FILE *fp)
{
int i=0;
fp = fopen("AGE.txt","r");
while(!feof(fp))
{
    int r;
    fscanf(fp,"%d",&r);
    if(i==10)
    {
      printf(" ");
      i=0;
    }
    printf("%d ",r);
    i+=1;
}
}

void function_four(int *start,int *end)
{
int i=0;
int *start1 = start;
while(start1!=end+1)
{
    if(i==9)
    {
      i=0;
      printf(" ");
      printf("%d ",*start1);
      start1+=1;
    }
     else
    {
       i+=1;
       printf("%d ",*start1);
       start1+=1;
    }
   }
}

void main() {
int ageArray[99];
fp = fopen("AGE.txt","a");
int *start = &ageArray[0];
int *end = &ageArray[98];
function_one(fp,99);
function_two(fp,start,end);
for(int i=0;i<99;i++)
{
    printf("%d ",ageArray[i]);
}
function_three(fp);
printf(" ")
function_four(start,end);
fclose(fp);
}