Question

In: Computer Science

1. Write a function named “Number” that reads in a list of numbers until the user...

1.

Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false.

2.

Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration:

struct Student {

string name;

int ID;

}

Student roster[30];

// Code to fill in the student info for all students.

// Sort the roster

// your code goes here

Solutions

Expert Solution

1.

Source Code:

Output:

Code in text format (See above images of code for indentation):

#include<iostream>
#include<iomanip>
using namespace std;
/*main function*/
int main()
{
   /*function prototype*/
   bool Number();
   bool result;
   /*function call*/
   result=Number();
   /*print results*/
   if(result)
       cout<<"You entered more even numbers than odd numbers";
   else
       cout<<"You entered more odd numbers than even numbers";
   return 0;
}
/*function prototype*/
bool Number()
{
   /*variables*/
   int n,even=0,odd=0;
   cout<<"Enter a number or 0 to exit:"<<endl;
   while(true)
   {
       /*read number from user*/
       cin>>n;
       /*if 0 then break*/
       if(n==0)
           break;
       /*check for even or odd*/
       if(n%2==0)
           even++;
       else
           odd++;
   }
   /*return true or false based on condition*/
   if(even>odd)
       return true;
   else
       return false;
}

2.

Source Code:

Output:

Code in text format (See above images of code for indentation):

#include<iostream>
#include<iomanip>
using namespace std;
/*structure declaration*/
struct Student
{
   /*members*/
   string name;
   int ID;
};
/*array of structures*/
Student roster[30];
/*main function*/
int main()
{
   /*variables*/
   int n,i,j;
   /*read number of students from the user*/
   cout<<"Enter number of students: ";
   cin>>n;
   /*read students data*/
   for(i=0;i<n;i++)
   {
       /*read student ID*/
       cout<<"Enter student "<<i+1<<" ID:";
       cin>>roster[i].ID;
       cin.get();
       /*read student Name*/
       cout<<"Enter student "<<i+1<<" Name: ";
       getline(cin,roster[i].name);
   }
   /*sort the roster array*/
   for(i=0;i<n-1;i++)
   {
       for(j=0;j<n-i-1;j++)
       {
           if(roster[j].ID>roster[j+1].ID)
           {
               /*swapping of ID's*/
               int temp=roster[j].ID;
               roster[j].ID=roster[j+1].ID;
               roster[j+1].ID=temp;
               /*swapping of Names*/
               string sn=roster[j].name;
               roster[j].name=roster[j+1].name;
               roster[j+1].name=sn;
           }
       }
       /*print id and names*/
       cout<<"ID\tName"<<endl;
       cout<<"-----------------------------"<<endl;
       for(i=0;i<n;i++)
       {
           cout<<roster[i].ID<<"\t"<<roster[i].name<<endl;
       }
   }
   return 0;
}


Related Solutions

Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input (other than the sentinel). Do the following: 1. Output the sum of all even numbers 2. Output the sum of all odd numbers 3. Output the count of all even numbers 4. Output the count of all odd numbers You must use alternation ('if' statements), loops and simple calculations to do...
Write a function named timesOfLetter that reads an array and returns the number of times of...
Write a function named timesOfLetter that reads an array and returns the number of times of each lowercase letter and each uppercase letter appear in it, using reference parameter. • Write a function named timesOfNumber that reads an array and returns the number of times of each odd number, and each even number appear in it, using reference parameter. • Write a function named isChar() that determines if the input is alphabetic or not during inputting. • Write a function...
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
Write a JavaScript program with a function named fives that reads two numbers from two text...
Write a JavaScript program with a function named fives that reads two numbers from two text fields and then outputs to a div "True" if both of the numbers are greater than 5 or if their sum is greater than 20. Otherwise your function should output "False" to the div. If you wish, you may use the following HTML code to begin your program.
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
IN C++ Write a program that reads in int values from the user until they enter...
IN C++ Write a program that reads in int values from the user until they enter a negative number like -1. Once the user has finished entering numbers, print out the highest value they’ve entered, the lowest value they’ve entered, and the total number of numbers they’ve entered. The negative number they entered should not be taken as one of the values entered.
Using C++ Write a program that reads a text from the keyboard until the user presses...
Using C++ Write a program that reads a text from the keyboard until the user presses the “Enter” key. Your program must count the number of uppercase alphabets (A through Z), lowercase alphabets (a through z), digits (0 through 9) and any other characters. The other character count value should NOT include the “Enter” key. Display the count on the screen. You must use the in-built C++ character I/O functions in this program.
First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT