1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Flauer [41]
3 years ago
9

Add Try/Catch error checking To make sure that the user enters valid values in your program. To catch negative values passed to

constructor. To catch if not enough memory is available for list. (Hint: bad_alloc)
Computers and Technology
1 answer:
ryzh [129]3 years ago
4 0

Answer:

int main()

{

 cout<<"Enter the size of array\n";

 int n;

 try{

 cin>>n;//get size of array

 if (n > 0){

 List list;//make object of list class

 try{

     list.init(n);//initialize the list

     list.fillData();//fill the data

     list.print();//print the data

     list.sortData();//sort the  data

     cout<<"\n--- After sorting the data---\n";

     list.print();//print the data

     cout<<"Enter the element to search\n";

     int x;

     cin>>x;//get element to search

     list.searchElement(x);//call the search function

 }

 catch (std::bad_alloc){

   cout<<"Sorry, could not allocate memory for list object.";

 }

 }else{

   throw('Negative Number detected');

 }

 }

 catch (int n){

   cout<<"The number should be a positive integer.";

 }

   return 0;

}

Explanation:

The try and catch keywords come in pairs, as they are used to control exceptions in the C++ source code. The try keyword checks for error in the source code given the condition (if the n integer variable is greater than 0). If the condition is met, the code in the try code block runs otherwise the catch keyword catches the error of a negative number (if the n variable in less than 0).

You might be interested in
Which of the following is true about simulation games? A. Simulation games involve competing in a sport against other players. B
WARRIOR [948]
<span>Simulation games recreate a real-world environment. </span>
6 0
3 years ago
Read 2 more answers
1. Write a static method named computeOddSum that is passed an array of int's named numbers. The method must compute and return
S_A_V [24]

Answer:

The method in Java is as follows:

public static int computeOddSum(int [] myarray){

       int oddsum = 0;

       for(int i=0; i<myarray.length; i++ ) {

        if(myarray[i]%2==1) {

           oddsum+=myarray[i];

        }

     }

     return oddsum;

   }

Explanation:

This defines the static method

public static int computeOddSum(int [] myarray){

This initializes oddsum to 0

       int oddsum = 0;

This iterates through the array

       for(int i=0; i<myarray.length; i++ ) {

This checks for odd number

        if(myarray[i]%2==1) {

The odd numbers are added, here

           oddsum+=myarray[i];

        }

     }

This returns the calculated sum of odd numbers

     return oddsum;

   }

To call the method from main, use:

<em>int [] myarray = {3, 10, 11, 2, 6, 9, 5};</em>

<em>      System.out.println("Odd sum = " + computeOddSum(myarray));</em>

5 0
2 years ago
Jax needs to write a block of code that will organize a list of items alphabetically. Which function should he use?
Zielflug [23.3K]
I would say sort since he’s sorting them into alphabetical order. I’m not 100% sure tho
4 0
3 years ago
Read 2 more answers
please write out an accurate code for the question below also please explain or I'll report you ( Thanks :) )
leva [86]

Answer: hmmmm i feel like u have to move the thingy thats shooting that light down 1 unit so it can move the planet.

Explanation: moving it down 1 unit will put it at the perfect angle to push the planet. :/

6 0
2 years ago
Read 2 more answers
What is the review of Rihanna feature of MS PowerPoint ​
mrs_skeptik [129]

rivebkovfscuindtbkbgn

8 0
3 years ago
Other questions:
  • The length of a hailstone sequence is the number of terms it contains. For example, the hailstone sequence in example 1 (5, 16,
    9·1 answer
  • Steps to log out of an email account ​
    5·2 answers
  • Hard light is usually not good for filming. True False
    11·2 answers
  • How do you check how much space is left in current drive?
    9·1 answer
  • A person's oral communication skills can give either a positive or negative first impression.
    12·1 answer
  • What is the different between the patch Tool and the Headling Brush tool?​
    5·1 answer
  • 3. Coaxial/telephone cable sends<br> during the data transmission<br> signal
    9·1 answer
  • Ray owns a gaming cafe where users can play video games on computers. These games involve rich graphics and need high processing
    5·1 answer
  • ANSWER ASAP!!! 12 POINTS!!!!! RIGHT ANSWERS ONLY
    7·2 answers
  • What technology is being used when you are sent an email saying you can track your package?
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!