Question

C++ Single Dimensional Arrays

Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.

 

The program defines method printGrades() that takes a signal-dimensional array of integer scores as a parameter and processes the scores to print letter grades based on the following scale:

 

     Grade is A if score >= 90 and score <= 100

     Grade is B if score >= 80 and score <= 89

     Grade is C if score >= 70 and score <= 79

     Grade is D if score >= 60 and score <= 69

     Grade is F if score < 60

 

Document your code and organized your output following these sample runs.

 

Sample run 1:

 

Class size:     5

Entered grades: 90, 67, 78, 89, 60

Student 0 score is 90 and grade is A

Student 1 score is 67 and grade is D

Student 2 score is 78 and grade is C

Student 3 score is 89 and grade is B

Student 4 score is 60 and grade is D

 

Sample run 2:

 

Class size:     3

Entered grades: 77, 50, 81

Student 0 score is 77 and grade is C

Student 1 score is 50 and grade is F

Student 2 score is 81 and grade is B


Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it returns false. The program main method calls method Compare()and prints the result from the method as shown below. Document your code and organized your output following these sample runs.

 

Sample run 1:

Array size:     4

First array:    23, -45, 78, 10

Second array:   23, -45, 78, 10
Judgment:       The arrays are identical

 

Sample run 2:

 

Array size:     5

First array:    78, 128, 300, 12, 300

Second array:   78, 128, 12, 300, 300
Judgment:       The arrays are not identical

 

Sample run 3:

 

Array size:     1

First array:    0

Second array:   0
Judgment:       The arrays are identical

 

Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows:

 

int arrayMax(int[] arr) returns the maximum value in the an array

int arrayMin(int[] arr) returns the minimum value in an array

void arraySquared(int[] arr) changes every value in the array to its square (value²)

void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9  becomes 9   8   7 )

 

The program main method creates a single-dimensional array of length 5 elements and initialize it with random integers between 1 and 100. The program displays the original array, then calls each of the above methods and displays their results as shown below. Document your code and organized your output following these sample runs.

 

Sample run 1:

 

Original array:  3, 5, 2, 6, 1

Max value:       6

Min value:       1

Squared array:   9, 25, 4, 36, 1

Reversed array:  1, 36, 4, 25, 9

 

Sample run 2:

 

Original array:  3, 2, 3, 7, 2

Max value:       7

Min value:       2

Squared array:   9, 4, 9, 49, 4

Reversed array:  4, 49, 9, 4, 9

 

Sample run 3:

 

Original array:  2, 2, 2, 2, 2

Max value:       2

Min value:       2

Squared array:   4, 4, 4, 4, 4

Reversed array:  4, 4, 4, 4, 4


0 0
Add a comment Improve this question Transcribed image text
Answer #1
Exercise #1 answer:
import java.util.Scanner;

public class AssignGrades {

    public static void printGrades(int[] scores) {
        int grade;
        char letterGrade;
        for (int i = 0; i < scores.length; i++) {
            grade = scores[i];
            if (grade >= 90) {
                letterGrade = 'A';
            } else if (grade >= 80) {
                letterGrade = 'B';
            } else if (grade >= 70) {
                letterGrade = 'C';
            } else if (grade >= 60) {
                letterGrade = 'D';
            } else {
                letterGrade = 'F';
            }
            System.out.println("Student " + i + " score is " + grade + " and grade is " + letterGrade);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("How many students are in class: ");
        int[] grades = new int[in.nextInt()];
        System.out.print("Enter " + grades.length + " grades: ");
        for (int i = 0; i < grades.length; i++) {
            grades[i] = in.nextInt();
        }
        System.out.println("Class size:     " + grades.length);
        System.out.print("Entered grades: ");
        for (int i = 0; i < grades.length; i++) {
            System.out.print(grades[i]);
            if (i < grades.length - 1) {
                System.out.print(", ");
            }
        }
        System.out.println();
        printGrades(grades);
    }
}

Add a comment
Answer #2
Exercise #2 Ans:

C++ Code

//Compare two arrays using user define function
#include 
using namespace std;
bool Compare(int arr1[],int arr2[]);
int n;
int main()
{
   bool r;
   cout<<"Array size :";
   cin>>n;
   int a[n],b[n];
   cout<<"First array :";
   for(int i=0;i>a[i];
   cout<<"Second array :";
   for(int i=0;i>b[i];

   r=Compare(a,b); //Compare function to check array a and b

    if(r==true) //compare function returns true
   cout<<"Judgment: The arrays are identical\n";
  
    else if(r==false) //compare function returns false
   cout<<"Judgment: The arrays are not identical\n";
  
   return 0;
}

//User define function to comapre two array with return type boolen
bool Compare(int arr1[],int arr2[])
{
int i;
for(i=0;i

Screen shot of code

Screen shot of Output

Add a comment
Answer #3
Exercise #3 Ans:

#include
#include
#include
using namespace std;
int arrayMin(int arr[],int size)
{
int minimum=arr[0];
for(int i=1;i {
if(arr[i] minimum=arr[i];
}
return minimum;
}
int arrayMax(int arr[],int size)
{
int maximum=arr[0];
for(int i=1;i {
if(arr[i]>maximum)
maximum=arr[i];
}
return maximum;
}
void arraySquared(int arr[],int size)
{
for(int i=1;i {
arr[i]=arr[i]*arr[i];
}
  
}
void arrayReverse(int arr[],int size)
{
int temp;
for(int i=1,j=size-1;i {
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
  
}


int main()
{
int arr[10];
//initalize array
srand(time(NULL));
for(int i=0;i<5;i++)
arr[i]=rand()%100 +1;
//print original array
cout<<"Original array : ";
for(int i=0;i<5;i++)
cout< //print maximum value of array
cout<<"\nMax value : "< //print minimum value of array
cout<<"\nMin value : "< //print squared array
cout<<"\nSquared Array : ";
arraySquared(arr,5);
for(int i=0;i<5;i++)
cout< //print reverse Array
cout<<"\nReveresed Array :";
arrayReverse(arr,5);
for(int i=0;i<5;i++)
cout<
return 0;
}

run 3

Add a comment
Answer #4
Exercise #1 Ans:


#include< iostream>
using namespace std;
void print_grades(int score[],int size)
{
for(int i=0;i< size;i++)
{
if(score[i]>=90)
cout<<"Student "<< i <<"score is "<< score[i]<<" and grade is A"<< endl;
else if(score[i]>=80)
cout<<"Student "<< i <<"score is "<< score[i]<<" and grade is B"<< endl;

else if(score[i]>=70)
cout<<"Student "<< i <<"score is "<< score[i]<<" and grade is C"<< endl;
else if(score[i]>=60)
cout<<"Student "<< i <<"score is "<< score[i]<<" and grade is D"<< endl;
else
cout<<"Student "<< i <<"score is "<< score[i]<<" and grade is F"<< endl;

}
}

int main()
{
int size,score[10],i;
cout<<"Enter class size :";
cin>>size;
for(int i=0;i< size;i++)
{
cout<<"Enter grade:";
cin>>score[i];
while(score[i]<0||score[i]>100)
{
cout<<"Invalid score !!! Try again :";
cin>>score[i];
}
}//loops ends after reading scores
print_grades(score,size);
  
}

Add a comment
Know the answer?
Add Answer to:
C++ Single Dimensional Arrays
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
  • C++ Arrays & Methods

     #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the same order. Otherwise, it...

  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE INTO VISUAL STUDIO Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns...

  • SOLVE IN PYTHON:Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes...

    SOLVE IN PYTHON:Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array....

  • SOLVE IN PYTHON: Exercise #2: Design and implement a program (name it CompareArrays) that compares the...

    SOLVE IN PYTHON: Exercise #2: Design and implement a program (name it CompareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array with integer values. The program defines method Compare() that takes two signal-dimensional arrays of type integer. The method compares the content of the arrays and returns true (Boolean type) if the arrays store same values in the...

  • Note: According to the question, please write source code in java only using the class method....

    Note: According to the question, please write source code in java only using the class method. Sample Run (output) should be the same as displayed in the question below. Make sure the source code is working properly and no errors. Exercise #2: Design and implement a program (name it compareArrays) that compares the content of 2 single-dimensional arrays of the same size. The program prompts the users to enter the array size. Then prompts the user to initialize each array...

  • IN PYTHON WITHOUT USING: .APPEND/ .SORT/ INSERT / .SPLIT Program 1: Design (pseudocode) and implement (source...

    IN PYTHON WITHOUT USING: .APPEND/ .SORT/ INSERT / .SPLIT Program 1: Design (pseudocode) and implement (source code) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional...

  • C++ ArrayMethods Program

    Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an arrayint arrayMin(int[] arr) returns the minimum value in an arrayvoid arraySquared(int[] arr) changes every value in the array to its square (value²)void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9  becomes 9   8   7 ) The program main method creates a single-dimensional array of length 5 elements and initialize it with random...

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • Please do in Java as simply as possible :) Exercise #3: Design and implement a program...

    Please do in Java as simply as possible :) Exercise #3: Design and implement a program (name it ArrayMethods), that defines 4 methods as follows: int arrayMax(int[] arr) returns the maximum value in the an array int arrayMin(int[] arr) returns the minimum value in an array void arraySquared(int[] arr) changes every value in the array to its square (value²) void arrayReverse(int[] arr) reverses the array (for example: array storing 7   8   9 becomes 9   8   7 ) The program main...

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
ADVERTISEMENT