Question

Assume the variable LIST is defined as a list of numbers. Write a code segment that...

Assume the variable LIST is defined as a list of numbers. Write a code segment that utilizes a loop to total up all of the numbers in the list up to, but not including, the first negative number. Store the result in the variable TOTAL. % test cases % LIST = [ 1 4 6 -2 7 9]; TOTAL = 11; % LIST = [ -2 4 5 7 9]; TOTAL = 0; % your code segment here %----------------------- % set the variable TOTAL to be the sum of the all numbers in LIST up to the first negative number

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

Please find below, the required code for the program giving the sum of all numbers in a LIST, upto the first negative number.

NOTE: Nothing was described in the question regarding the coding language, so I have written the code in C++.

//start of code

#include <iostream>
using namespace std;

int main() {
   cout << "Enter the number of elements: ";
int length;
cin >> length;

int *LIST = new int[length];
  
for(int i=0; i<length;i++){
   cin>>LIST[i];
}
  
/*int LIST[6] = {1,4,6,-2,7,9}; //uncomment this, if static input is required and comment the above code
int length = 6; //and change the input data according to your need*/
int TOTAL=0;
for(int i=0;i<length;i++){ //loop through length of list to add each elements value to TOTAL
   if(LIST[i]<0){ //if value is less than zero, display total and exit the program
       cout<<"TOTAL = "<<TOTAL;
       return 0;
   }
   else{
       TOTAL = TOTAL + LIST[i]; //otherwise, add the element value to TOTAL
   }
}
cout<<"TOTAL = "<<TOTAL; //display TOTAL
   return 0;
}

//end of code

Hope this solves your problem, and if it does, please give this answer a thumbs up. Thank you.

Add a comment
Know the answer?
Add Answer to:
Assume the variable LIST is defined as a list of numbers. Write a code segment that...
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
  • write a code segment for a loop to calculate the sum of first 10 odd numbers...

    write a code segment for a loop to calculate the sum of first 10 odd numbers [what your program computes is 1+3+5+7+9+11+13+15+17+19. store the result in register r0.] it’s assembly language

  • Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that...

    Question 1a - Increasing Numbers in List - First Occurence(3 points) Write a function numIncreasing1(L) that takes as input a list of numbers and returns a list of the first sequence within that is increasing order, and has a length of at least 2. If no increasing sequential numbers are found, (ie. the input list is in descending order) then naturally a list of just the first value is returned. Increasing sequence means for a number to be valid it...

  • Assume that, there is no strlen() function defined for us. Write a code segment using while...

    Assume that, there is no strlen() function defined for us. Write a code segment using while loop that will calculate the length of the following declaration. char stringVar[10] = “Hello”; Note that, the length of the C-string variable stringVar is 5. Can someone help me with this?

  • C++ program Write a code segment to find the max element in each row and then...

    C++ program Write a code segment to find the max element in each row and then store the values to the "result" array. For example, { {2, 7, 9, 6, 4}, => 9 {6, 1, 8, 10, 4}, => 10 {4, 3, 7, 2, 9}, => 9 {9, 9, 0, 3, 1}, => 9 {8, 8, 7, 8, 9}, => 9 {1, 2, 1, 2, 3} } => 3 the result array has the values [9, 10, 9, 9, 9,...

  • Python please help! Thanks you Write a code to get an unlimited number of grades from...

    Python please help! Thanks you Write a code to get an unlimited number of grades from the user (the user can press enter to finish the grades input, or use a sentinel, for example-1), and then calculate the GPA of all the grades and displays the GPA To do this you might need some help. Try to follow the following process (and check your progress by printing different values to make sure they are as they supposed to be): 1-...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the...

    Using R code only 4. The Fibonacci numbers are the sequence of numbers defined by the linear recurrence equation Fn F-1 F-2 where F F2 1 and by convention Fo 0. For example, the first 8 Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21. (a) For a given n, compute the nth Fibonnaci number using a for loop (b) For a given n, compute the nth Fibonnaci number using a while loop Print the 15th Fibonacci number...

  • I need this code in java. Loops do just what they sound like they should -...

    I need this code in java. Loops do just what they sound like they should - they perform a statement again and again until a condition is fulfilled. For this lab you will be practicing using loops, specifically a for loop. The for loop takes the form of: for(/*variable initialization*/;/*stop condition*/; /*increment*/){ //statements to be done multiple times } Task Write an application that displays every perfect number from 2 through 1,000. A perfect number is one that equals the...

  • Write a C program to sum up all the odd numbers between a lower limit number...

    Write a C program to sum up all the odd numbers between a lower limit number and an upper limit number provided by a user. The requirements are: First, request the user to provide a lower limit integer number and an upper limit integer number that is larger than the lower limit number, and save them in variables lowerLimit and upperLimit, respectively. If the user has entered an upper limit number (upper Limit) that is NOT larger than the lower...

  • 1) Which of the following is NOT true about a Python variable? a) A Python variable...

    1) Which of the following is NOT true about a Python variable? a) A Python variable must have a value b) A Python variable can be deleted c) The lifetime of a Python variable is the whole duration of a program execution.   d) A Python variable can have the value None.        2) Given the code segment:        What is the result of executing the code segment? a) Syntax error b) Runtime error c) No error      d) Logic error 3) What...

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