Question

In: Computer Science

Please answer these The following array is declared: int grades[20]; a. Write a printf() statement that...

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

Solutions

Expert Solution

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******************


Related Solutions

Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to...
Can you please write a pseudocode for the following: #include <stdio.h> int main(){    printf("Welcome to my Command-Line Calculator (CLC)\n");    printf("Developer: Your name will come here\n");    printf("Version: 1\n");    printf("Date: Development data Will come here\n");    printf("----------------------------------------------------------\n\n");    //choice stores users input    char choice;    //to store numbers    int val1,val2;    //to store operator    char operation;    //flag which leths the loop iterate    //the loop will break once the flag is set to 0...
Using the following array: //may be declared outside of the main function const int NUM_Games =4;...
Using the following array: //may be declared outside of the main function const int NUM_Games =4; //may only be declared within the main function int scores[NUM_GAMES] = {122, 76, 92, 143}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the scores 2) Change a score 3) Display game with the highest score 4) Display a sorted list of the scores 5) Quit Write a function called getValidScore that allows a user to enter...
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
answer in c++ , Write the definition of the function NthOccurrence() whose header is int NthOccurrence(Array&...
answer in c++ , Write the definition of the function NthOccurrence() whose header is int NthOccurrence(Array& data,const T& value,int n) template It returns the index of the nth occurrence of value in data. If n is not positive, value appears less than n times in data or data is empty, it returns -1.
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT