Question

In: Computer Science

Part II: Programming Questions NOTE: CODING LANGUAGE IS C# PQ1. How would you declare the following...

Part II: Programming Questions

NOTE: CODING LANGUAGE IS C#

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.

Solutions

Expert Solution

Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

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

Answer: We can declare them like below.

int counter;
double sum;
double average;

//declaring an array that is capable of storing 32 grades.

double[] grades=new double[32]; //or simply double[] grades; if you want to initialize later

PQ2.

i. 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?

Answer: You can initialize the grades array like below. Replace the numbers with the grades you need.

grades=new double[]{3,3.5,4,4,1,2,3,4,5,3.4,4,1,2.3,3,4,2.5,2.5,3,3,3,3,3,3,4,2.3,4,4,4,2,3.9,3,3};
        

ii. Instead of initializing the array above, how would you, in a loop, accept input from the user for the 32 grades?

Answer: You can initialize the grades array using a loop, accepting input from user like below

        //looping from i=0 to i=grades.Length-1 (32 in this case)
        for(int i=0;i<grades.Length;i++){
            //asking for the grade
            Console.WriteLine("Enter grade "+(i+1)+": ");
            //reading a grade, parsing to double, adding to index i in grades array
            grades[i]=double.Parse(Console.ReadLine());
        }

PQ3.

i. How would you calculate the sum of all the grades in the array above?

        sum=0;
        //looping from i=0 to i=grades.Length-1 (32 in this case)
        for(int i=0;i<grades.Length;i++){
            //adding current grade to sum
            sum+=grades[i];
        }

ii. How would you average the sum above?

        //dividing sum by number of grades to get the average
        average=(double) sum/grades.Length;

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.

//method to check if n is prime, return true if prime, else false
    public static bool IsPrime(int n){
        //any number below 2 is not prime
        if(n<2){
            return false;
        }
        //looping from 2 to n-1
        for(int i=2;i<n;i++){
            //if i divides n evenly, then n is not prime
            if(n%i == 0){
                return false; //not prime
            }
        }
        //if the program reach this point, then n is prime.
        return true;
    }


Related Solutions

Complete the following assignment in C programming language. 1. Declare the following float variables: -Maximum exam...
Complete the following assignment in C programming language. 1. Declare the following float variables: -Maximum exam score, user's exam score, and percentage. 2. Ask the user to input data into your variables, such as: -"What is the max score of your exam:" -"What was your score:" 3. Use if statements to validate user inputs. For example, score received should not be more than maximum possible score. -Display an error message when the user enters invalid data. -You can restart the...
Note: I need a code and other requirement Note: programming language is c++ If you need...
Note: I need a code and other requirement Note: programming language is c++ If you need more information, please clarify what information you want. consider solving the equation sin(x) - e^(-x) = 0 write a computer program to solve the given equation using: 1- bisection method 2- fixed-point method 3- newton's intervals: {0,1},{1,2},{2,3},{3,4},{4,5},{5,6},{6,7},{7,8},{8,9},{9,10} choose accuracy E = 10^(-5) Make sure you document your program Requirement : 1- Mathematical justification 2- algorithem description 3- code (program) with documentation 4-output: roots ,...
C Programming- How would the whole part 1 be coded with multiple loops? Part 1: You...
C Programming- How would the whole part 1 be coded with multiple loops? Part 1: You can do A, B, and C in one program with multiple loops (not nested) or each one in a small program, it doesn’t matter. A. Create a loop that will output all the positive multiples of 9 that are less than 99. 9 18 27 36 45        …. B. Create a loop that will output all the positive numbers less than 200 that are...
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions....
In C++ Programming Language: 1a. Declare a class, namely ReverseUniverse, that contains three public member functions. Please see Q2 and Q3 for the name of the second and third function. (5 points) Write a function string reverseString that reverses a string (first member function). It takes a string parameter and returns its reversed version. (10 points) 1b. In the class ReverseUniverse in Q1. Write a function vector reverseVector that reverses a vector (second member function). It takes a double vector...
1. Provide brief answers to the following questions: a) What is preprocessing in C programming language?...
1. Provide brief answers to the following questions: a) What is preprocessing in C programming language? Cite 3 examples of preprocessor directives in C. b) What is the main difference between Heap and Stack memory regions? c) What is the difference between Stack and Queue? d) What is the purpose of a compiler? Name the C compiler that you used for this course? e) What is a pointer variable in C? 2. int var; Use this variable to write a...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences...
please answer with coding from The second edition C programming language textbook /* Changes all occurrences of t in s to u, result stored in v * * Example: * * char v[100]; * replace("hello", "el", "abc", v) => v becomes "habclo" * replace("", "el", "abc", v) => v becomes "" (no change) * replace("hello", "abc", "def", v) => v becomes "hello" (no change) * replace("utilities", "ti", "def", v) => v becomes "udeflidefes" * */ void replace(char *s, char *t,...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right?...
Running laps: This is C++ Programming Coding Language Everyone likes running laps in gym class right? There are 5 objectives and 20 points each. Please indicate in code (or comments) where each objective begins and ends. It may be prudent to place each objective in it's own function. In this lab you will be reading in a file of student results. The students each ran 3 laps in a race and their times to complete each lap are posted in...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
In C programming language how do you find if all the character on a single line...
In C programming language how do you find if all the character on a single line in a 2 dimensional array are the same? The program should read line by line to check if all the characters on a line are the same. For this program we want to see if the * character ever shows up on a line where it is the only character on the line. as seen below. so lets say we have a array with...
In the java programming language. How would you find if THREE (3) numbers in an array...
In the java programming language. How would you find if THREE (3) numbers in an array add up to a certain sum so for example if my array element consists of: INPUT: 200, 10, 50, 20 and my output asks if these elements add to: 270? YES (200+50+70) 260? YES (200+10+50) 30? NO What method would you suggest to look through the elements in an array (which im going to add through a text file) and determine if these sums...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT