Question

In: Computer Science

Consider a list called A: A = [-6, 10, 100, 5, -20, 1000, 9, -15] Make...

Consider a list called A:

A = [-6, 10, 100, 5, -20, 1000, 9, -15]

Make a for loop that iterates over A and:

  • If number is negative, print the square of the number.
  • if number is positive but less than or equal to 100, print the number itself.
  • if number is positive but greater than 100, print 0.

Solutions

Expert Solution

HELLO

AS YOU HAVE NOT SPECIFIED ANY LANGUAGE I AM ADDING SOLUTION WITH OUTPUT IN PYTHON AND C

PLEASE GO THROUGH IT AND IF YOU HAVE ANY DOUBT PLEASE FEEL FREE TO ASK IN COMMENT

AND IF THIS HELPS YOU PLEASE GIVE UPVOTE

CODE (C)


#include <stdio.h>

int main()
{
    //DECLARING ARRAY WITH GIVEN LIST
    int A[8]={-6,10,100,5,-20,1000,9,-15};
    int i;
    //ITERATING THROUGH LOOP
    for(i=0;i<8;i++)
    {
        int display = 0;
        //IF ELEMENT IS NEGATIVE
        if(A[i]<0)
            display = A[i]*A[i];
        //IF ELEMENT IS POSITIVE AND LESS THAN OR EQ TO 100
        else if(A[i]<=100)
            display = A[i];
        //ELSE ELEMENT IS GREATER THAN 100
        printf("%d  ",display);
    }
    return 0;
}

OUTPUT (C):

CODE (PYTHON)

def printNum(list):
    #ITERATING THROUGH LOOP
    for i in list:
        display =0
        #IF ELEMENT IS NEGATIVE
        if(i<0):
            display = i*i
        #IF ELEMENT IS POSITIVE AND LESS THAN OR EQ TO 100
        elif(i<=100):
            display = i
        #ELSE ELEMENT IS GREATER THAN 100
        print(display)
        
list = [-6,10,100,5,-20,1000,9,-15]
printNum(list)

OUTPUT (PYTHON)


Related Solutions

10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25?...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25? = 200 25a − 15? − 5? = 300 10? + 20? − 30? + 100? = 400 how to do flowchart using gauss elimination and lu decomposition method
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25?...
10? + 50? + 20? + 10? = 100 5? + 15? + 75? − 25? = 200 25a − 15? − 5? = 300 10? + 20? − 30? + 100? = 400 how to write coding in matlab using lu decomposition
Make a C++ program that outputs these following numbers: 10 5 9 10 8 15 7...
Make a C++ program that outputs these following numbers: 10 5 9 10 8 15 7 20 6 25 Please do not use functions. Only use while loop. Thank you :)
a. Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112,...
a. Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1} using Insertion Sort and determine the total number of comparisons made (do not count swaps) b. Sort the list stated in 5a) but using Merge Sort
Build a Max Heap using the following: A= {20, 15, 10, 14, 8, 6, 9}
Build a Max Heap using the following: A= {20, 15, 10, 14, 8, 6, 9}
1. Consider the following data set: D= (5, 10, 15, 15, 5, 10, 15, 15, 5,...
1. Consider the following data set: D= (5, 10, 15, 15, 5, 10, 15, 15, 5, 10, 15, 15) SD= 4.33 How would you add a number to this set while keeping the SD the same? 2. tossing a coin 50 times A. from the 50 flips compute the proportion of heads from your 50 flips. B. For a 95% confidence level find the z critical value C. compute the 95% confidence interval for p, the margin of error from...
Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1}...
Sort the list A[ ]={ 20, 13,4, 34, 5, 15, 90, 100, 75, 102, 112, 1} using Insertion Sort and determine the total number of comparisons made (do not count swaps) b.Sort the list stated in 5a) but using Merge Sort
Consider the grouped frequency distribution. Class Limits 3-5 6-8 9-11 12-14 15-17 f 4 10 10...
Consider the grouped frequency distribution. Class Limits 3-5 6-8 9-11 12-14 15-17 f 4 10 10 5 10 (a) Find the mean. (Give your answer correct to two decimal places.) (b) Find the variance. (Give your answer correct to two decimal places.) (c) Find the standard deviation. (Give your answer correct to two decimal places.)
Consider the following data: X Y 1 13 3 10 5 9 5 5 6 3...
Consider the following data: X Y 1 13 3 10 5 9 5 5 6 3 Draw a Scatter Plot of the data. Do you believe the correlation coefficient r will be positive, negative or close to zero? Why What is your estimate to the value of Y associated with X=4?
C++ 1) Given Arr[10] = {7, 9, 13, 15, 16, 10, 12, 5, 20, 27} Write...
C++ 1) Given Arr[10] = {7, 9, 13, 15, 16, 10, 12, 5, 20, 27} Write a program to count number of EVEN and ODD items. Do a screen output of your result. 2) Given Arr[10] = {7, 9, 13, 15, 16, 10, 12, 5, 20, 27} Write a program to construct array ODD[] and EVEN[] from Arr[10] as you realize ODD[] consists of odd number and EVEN[] consists of even number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT