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...
please write it in printf and scanf please Please write it in c# program please and...
please write it in printf and scanf please Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches. Your function will convert the weight and height into Body Mass...
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.
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
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...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: //...
public class SumMinMaxArgs { private int[] array; // You will need to write the following: // // 1. A constructor that takes a reference to an array, // and initializes an instance variable with this // array reference // // 2. An instance method named sum, which will calculate // the sum of the elements in the array. If the array // is empty (contains no elements), then this will // will return 0. You will need a loop for...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT