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

Please write in #include <stdio.h> #include <string.h> DO NOT WRITE IN <IOSTREAM

ID: 3555401 • Letter: P

Question

Please write in

#include <stdio.h>

#include <string.h>

DO NOT WRITE IN <IOSTREAM>

Create an array name Ten holding 20 elements that are multiples of 10. E.g. [10, 20, 30,40, ..., 200]. Write every other element (i.e. 2nd(20), 4th(40), 6th(60), etc.) in the array to a file name Ten.txt and close the file. Re-open the file Ten.txt and append the remaining elements (1st (10), 3rd(30), 5th50), etc.) to it. Then close the file. Read the contents of the file Tentxt into another array named TenCopy and display the contents of TenCopy.

Explanation / Answer

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

int main(){
FILE *fp;

int Ten[] ={10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200};
int i;
int TenCopy[20];
fp = fopen("Text.txt","w+");

for(i=0; i<20; i++){
if(i%2 == 1){
fprintf(fp,"%d ",Ten[i]);
}
}
fclose(fp);

fp = fopen("Text.txt","a+");

for(i=0; i<20; i++){
if(i%2 == 0){
fprintf(fp,"%d ",Ten[i]);
}
}
fclose(fp);

fp = fopen("Text.txt","r+");

for(i=0; i<20; i++){
fscanf(fp,"%d",&TenCopy[i]);
}
fclose(fp);

for(i=0; i<20; i++){
printf("%d ",TenCopy[i]);
}

return 0;
}