In: Computer Science
Please answer these
The following array is declared: int grades[20];
a. Write a printf() statement that can be used to display values of the first, third, and seventh elements of the array.
b. Write a scanf() statement that can be used to enter values into the first, third, and seventh elements of the array.
c. Write a for loop that can be used to enter values for the complete array. d. Write a for loop that can be used to display values for the complete array.
Write a C function reverse() that reverse the values in the input array. The array is assumed to be of integer type. The function reverse() should have two arguments. The first argument is the integer array and the second is the number of elements in the array
Write a C program that performs the following tasks: a. Stores the following numbers into a file named result.dat: 16.25, 18.96, 22.34, 18.94, 17.42, 22.63. You need to open the file for write only. Do not forget to check if the file open is successful or not. After writing is complete, close the file. b. Open the file result.dat for read only. Read, compute and display the sum and average of the data. Your program should check if the fopen() is successful as well as use a while statement that uses the EOF marker as a sentinel to read the data
1)ARRAY PROGRAM
CODE:


OUTPUT:
Raw_code:
//including the stdio.h for input and output function
#include<stdio.h>
//function prototypr for reverse with parameters an integer array
and an integer variable
void reverse(int *,int );
int main()
{
   //integer array grades with size 20
   int grades[20];
   int i;
   //b
   //scanf statement that can be used to enter the values
into the first , third and seventh
   //elements of the array
   printf("Enter the first , third and seventh
values:");
   scanf("%d %d
%d",&grades[0],&grades[2],&grades[6]);
   //a
   //printf statement that can be used to display the
values of first, third and seventh elements of
   //the array
   printf("\nthe first element is %d\nthe second element
is %d\nthe seventh element is
%d\n",grades[0],grades[2],grades[6]);
   //c
   //for loop that can be used to enter the values for
complete the array
   for(i=0;i<20;i++)
   {
       printf("Enter the value for
grades[%d]:",i);
       scanf("%d",&grades[i]);
   }
   //d
   //for loop that displays the values for the complete
the array
   for(i=0;i<20;i++)
   {
      
printf("\ngrades[%d]:%d",i,grades[i]);
   }
   int size=20;
   //calling reverse function with grades array's base
address and size of the grades array
   reverse(grades,size);
   //display the array elements after calling reverse
function
   printf("\nThe array elements:");
   for(i=0;i<20;i++)
   {
      
printf("\ngrades[%d]:%d",i,grades[i]);  
   }
   return 0;
}
//function definition to reverse the array elements
void reverse(int a[],int n)
{
   //declaring an integer array with size n
   int b[n];
   int i,j;
   //logic to store reverse the array
   for(i=n-1,j=0;i>=0;i--,j++)
   {
       b[j]=a[i];
   }
   for(i=0;i<n;i++)
   {
       a[i]=b[i];
   }
}
2)
CODE:
FILE PROGRAM
Raw_code:
//including stdio.h for input and output functions
#include<stdio.h>
//including stdlib.h for FILE manipulating functions and exit
functions
#include<stdlib.h>
//starting of main function
int main()
{
   //decalring FILE pointer
   FILE *fptr;
   //store the values in an array
   double
result[]={16.25,18.96,22.34,18.94,17.42,22.63};
   //opening a program.dat file in write mode
   fptr=fopen("program.dat","w");
   //if there is no file is found with that name a new
file is created
   if(fptr==NULL)
   {
       printf("Error");
       exit(1);
   }
   //if the file exists
   else
   {
       //integer variable size to store
the size of results array
       int
size=sizeof(result)/sizeof(double);
       //integer variable i for iteration
purpose
       int i;
       for(i=0;i<size;i++)
       {
           //storing the
values in the result.dat
          
fprintf(fptr,"%.2lf ",result[i]);
       }
   }
   //writing is complete then closing the file
   fclose(fptr);
   //open the result.dat file in read mode
   //if there is no file is found then it exit from the
program with appropriate message
   if((fptr=fopen("program.dat","r"))==NULL)
   {
       printf("Error opening file");
       exit(1);
   }
   //if the file is found then else statement is
executed
   //compute and display the sum and average
   else
   {
       //double varaible to store the
sum
       double sum=0;
       //double variable to store the
average
       double average;
       //double varaible to store the
reading number from file
       double num;
       //double variable to store the
number of integers in tge file
       double count;
       //while statement that uses the EOF
marker as a sentinel to read the data
       while(fscanf(fptr,"%lf",&num)
!= EOF)
       {
           //store the sum
of numbers
           sum = sum +
num;
           //incrementing
the count
           count = count +
1;
           //printing the
each value that we read from the file
          
printf("\nvalue:%lf",num);
       }
       //computing the average
           average = sum /
count;
           //displaying the
sum and average
           printf("\nSum =
%lf Average = %lf\n",sum,average);
           //close the
file
          
fclose(fptr);
   }
   //return 0 to the operating system after successful
compilation
       return 0;
}
**************For any queries comment me in the comment box******************