Question

The program in C language PQ1. How would you declare the following scalar and non-scalar (vector)...

The program in C language

PQ1. How would you declare the following scalar and non-scalar (vector) variables?

  1. counter, an integer
  2. sum, a decimal number
  3. average, a decimal number
  4. grades, an array of 32 decimal numbers

PQ2.

  1. How would you initialize the array above with numbers between 0.0 and 4.0 representing the grades of all courses a senior has received in the four years of college?
  2. Instead of initializing the array above, how would you, in a loop, accept input from the user for the 32 grades?

PQ3.

  1. How would you calculate the sum of all the grades in the array above?
  2. How would you average the sum above?

PQ4. The algorithm for checking if a number is a prime is to check if it is divisible by any number less than itself other than 1. (Divisibility implies that if you use the modulus operator, %, the result will be 0). Write a function to check if a number is a prime.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PQ1:

int counter;

This is how you declare an integer variable named counter.

In general to declare an integer by the name variable_name , the syntax is int variable_name;

double sum;

This is how you declare a decimal number named sum.

In general to declare a decimal number by the name variable_name , the syntax is double variable_name;

Similarly for decimal number average we declare it as

double average;

There is another data type that can also be used instead of double namely float but double carries with it the advantage of being 2x more precise in the fractional part as compared to float.

double grades[32];

This is the way to declare an array of 32 decimal numbers , wherein the first grade can be accessed using grades[0], second by grades[1] and so on until the 32nd grade can be accessed by grade[31]. This shows that arrays are 0-indexed in C.

In general to declare an array of decimal numbers of size n , we simply write double array_name[n];

PQ2

To initialize the above grades array we use the following syntax:

double grades[32] = {2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5 };

Simply place 32 decimal numbers inside brackets to initialize the array.

The simplest way to initialize the array.

But most often in practical programs , this is not the case and it is the user who provides the contents of the array.In such cases following is the way to implement it:

for(int i=0;i<32;i++){

          scanf("%lf",&grades[i]);

}

This is how you use a for loop to accept grades from the user and store them in the array.

The program which uses the first approach to initializing arrays and prints the contents of the array:

#include<stdio.h>

int main(){

   double grades[32] = {2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5, 2.0, 3.0, 4.0, 2.5,2.0, 3.0, 4.0, 2.5 };

   for(int i=0;i<32;i++){
       printf("%lf ",grades[i]);
   }
   printf("\n");

   return 0;

}

OUTPUT:

The program which uses the second approach to initializing arrays and prints the contents of the array:

#include<stdio.h>

int main(){

   printf("Please enter 32 grades: ");

   double grades[32];
   for(int i=0;i<32;i++){
      
              scanf("%lf",&grades[i]);

   }
   for(int i=0;i<32;i++){
       printf("%lf ",grades[i]);
   }
   printf("\n");

   return 0;

}

OUTUPUT:

PQ3

To calculate the sum of grades array we can just use a for loop and store the sum of contents of the decimal numbers in a decimal variable sum.

double sum = 0;

for(int i=0;i<32;i++){

       sum = sum+grades[i];

}

After this iteration sum contains the sum of all grades of array.

The program which calculates sum over the grades array and prints the sum:

#include<stdio.h>

int main(){

   printf("Please enter 32 grades: ");

   double grades[32];
   for(int i=0;i<32;i++){
      
              scanf("%lf",&grades[i]);

   }
   double sum = 0;

   for(int i=0;i<32;i++){

           sum = sum+grades[i];

   }

   printf("The sum of the grades is: %lf\n",sum);

   return 0;

}

OUPTUT:

The average can be easily computed using the sum previously calculated and by dividing it simply by 32.0

double average = sum/32.0;

Now average stores the average of grades of the array.

The program which calculates average over the grades array and prints the average:

#include<stdio.h>

int main(){

   printf("Please enter 32 grades: ");

   double grades[32];
   for(int i=0;i<32;i++){
      
              scanf("%lf",&grades[i]);

   }
   double sum = 0;

   for(int i=0;i<32;i++){

           sum = sum+grades[i];

   }


   double average = sum/32.0;
   printf("The average of the grades is: %lf\n",average);

   return 0;

}

OUTPUT:

In PQ2 and PQ3 programs and output have been provided just for understanding purposes.

PQ4

#include<stdio.h>

int isPrime(int n){
    //HANDLING BASE CASE THAT 1 IS NEITHER PRIME NOR COMPOSITE.

    if(n==1)return 0;


   int check = 1;
   //CHECK IF n IS DIVISIBLE BY ANY NUMBER BETWEEN 2 AND N-1(INCLUSIVE)
   //IF SO THEN n IS NOT PRIME AND THUS SET CHECK = 0 AND BREAK
   for(int i=2;i<n;i++){
       if(n%i == 0){
           check = 0;
           break;
       }
   }

   //IF CHECK IS 1 STILL IT MEANS THERE IS NO NUMBER OTHER THAN 1 AND n THEMSELVES
   //WHICH DIVIDE n THUS n IS PRIME ELSE NOT
   return check;
}

int main(){
   int n;
   printf("Enter a number to be checked if it is prime or not:");      
   scanf("%d",&n);
   if(isPrime(n)==1){
       printf("The entered number is prime\n");
   }else{
       printf("The entered number is not prime\n");
   }
   return 0;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
The program in C language PQ1. How would you declare the following scalar and non-scalar (vector)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive...

    Lottery Game (15 Numbers). Design and implement a C++ program that generates 15 random non-repeating positive integer numbers from 1 to 999 (lottery numbers) and takes a single input from the user and checks the input against the 15 lottery numbers. The user wins if her/his input matches one of the lottery numbers. Your implementation must have a level of modularity (using functions) Functions you need to implement: Define function initialize() that takes array lotterNumbers[] as a parameter and assigns...

  • How would i code this in MatLab 3. Write the program, openclasses.m, where you start by...

    How would i code this in MatLab 3. Write the program, openclasses.m, where you start by creating a structured array called class which contains the information in the following table (note: seats should be a single fieldname with a (1x2) array inside it). Class Title Introduction to Engineering Computers in Psychology Writing and Rhetoric Beginning Spanish Introduction to Africana Studies Number Seats (Max, Enrolled) 10111 (45, 32) 20000 (28, 25) 12100 (14, 14) 10101 (19,13) (36, 32) 20082 From this...

  • (Done in Eclipse Java) 1. Given an integer between 1—100 captured from a user, perform the...

    (Done in Eclipse Java) 1. Given an integer between 1—100 captured from a user, perform the following conditional actions: • If is odd, print Weird • If is even and in the inclusive range of 2 to 5, print Not Weird • If is even and in the inclusive range of 6 to 20, print Weird • If is even and greater than 20, print Not Weird Note: Validate that your algorithm works for all cases. 2. Read an integer...

  • C programming Ask the user how many numbers they would like generated. Your program will then...

    C programming Ask the user how many numbers they would like generated. Your program will then generate that many numbers (between 0 and 1000, inclusive) into an array of the same size. Find the smallest and largest numbers in the array, printing out the values and locations of those numbers in the array. Then, find the sum and average of the numbers in the array. Finally, print out the entire array to verify. You MUST use functions where appropriate!

  • c# Write compile and test a program named IntegerStatistics. The programs Main() method will: 1. Declare...

    c# Write compile and test a program named IntegerStatistics. The programs Main() method will: 1. Declare an array of 10 integers. 2. Call a method to interactively fill the array with any number of values (up to 10) until a sentinel value is entered. [If an entry is not an integer, continue prompting the user until an integer is entered.] When fewer than 10 integers are placed into the array, your statistics will be off unless you resize the array...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in...

    Q1) How would you declare an array of doubles called myDoubles? Arrays can be initialized in one of two ways. In one method, the array elements are placed in a list enclosed in curly braces after the array name definition. For example, the code below creates an array of ints with 3 elements: 1, 2 and 3. int[] a = {1, 2, 3, 4}; We can also initialize an array with a new construct, indicating how many elements we want...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Prime Number Programing in C Note: The program is to be written using the bitwise operation....

    Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT