Question

Write a C++ program - create a 1-d array. Its data type is integer. It has...

Write a C++ program - create a 1-d array. Its data type is integer. It has 10 elements. - initialize this array by 10 integer numbers. Five elements of the array are positive, and others are negative. - use a while loop to manipulate the array as below. - If the value of an element is positive, the value is doubled. Otherwise, the value is incremented by 3. - print the new value of each element. - use a for loop to calculate the sum of all elements. - calculate the average of all elements

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int main() {
    int size = 10;
    int arr[] = {4, -1, -5, -2, 9, 4, 7, -6, 5, -3};
    int i = 0;
    while (i < size) {
        if(arr[i] > 0) {
            arr[i] *= 2;
        } else {
            arr[i] += 3;
        }
        i++;
    }
    int sum = 0;
    for (int j = 0; j < size; ++j) {
        cout << arr[j] << " ";
        sum += arr[j];
    }
    cout << endl;
    double average = sum / (double)size;
    cout << "Sum = " << sum << endl;
    cout << "Average = " << average << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program - create a 1-d array. Its data type is integer. It has...
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++. Write a program that copies the contents of one array into another array but in...

    C++. Write a program that copies the contents of one array into another array but in reverse order using pointers.           - in main()                    - define 2 arrays of type int, 10 elements each                              - initialize one array with numbers 1 through 10                              - initialize all elements of the second array to 0                    - call function reverseCopy with both arrays as arguments                    - display the values of array number 2 (reversed order)           - reverseCopy...

  • write a java program Accept a positive integer n from keyboard and then create an array...

    write a java program Accept a positive integer n from keyboard and then create an array or arraylist containing n random elements within the range [-n,n). Print out the random array or arraylist, and then find out and print out the number of inversions and the maximum subarray (index range of the maximum subarray along with the maximum subarray sum) in the array or arraylist using divide and conquer, respectively. For example, suppose we accept integer 6 from keyboard, then...

  • C++ This week, you are to create two separate programs, first using a simple array and...

    C++ This week, you are to create two separate programs, first using a simple array and the second using a 2D-array (matrix). [Part 1] Write a program that accepts exactly ten (10) integer numbers from the user and stores them in an array. In a separate for-loop, the program then goes through the elements in the array to print back: (i) The sum of the 10 numbers, (ii) the minimum value from the 10 numbers, and (iii) the maximum value...

  • 3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and pr...

    3. (Dynamic Array) In C++, the largest int value is 2147483647. So. an integer larger than this cannot be stored and processed as an integer. Similarly, if the sum or product of two positive integers is greater than 2147483647, the result will be incorrect. One way to store and manipulate large integers is to store each individual digit of the number in an array. Your program must, at least, contain a function to read and store a number into an...

  • Class DemoArray: 1. Create an array of type String , name it words, and initialize it...

    Class DemoArray: 1. Create an array of type String , name it words, and initialize it with the following words: one, two, three, four, five, four, three, two, one 2. Use a method of class Arrays to print the array. Make sure to use a label 3. At this point we are going to fix the misspelled word three. Use a loop to access the elements of the array. One by one check whether the current element happens to be...

  • Write code that declares an array of integer values that will represent the first five even...

    Write code that declares an array of integer values that will represent the first five even numbers: 2, 4, 6, 8, 10. Add code to initialize each element of the array with the even number values shown above. Add code to calculate the product of the array elements, and store the result in a variable named product. You must access the array elements to accomplish this. Do not write product = 2 * 4 * 6 * 8 * 10;...

  • Write a program that performs the following operations on a one dimensional array with 50 unsigned...

    Write a program that performs the following operations on a one dimensional array with 50 unsigned integers. The main program will initialize the array, print the array values to the screen and then call a function that will determine and print the maximum and minimum values. •Declare the array and any other variables. •Use a loop to initialize the array with 50 random integer values between 0 and 99 using the rand() function. (number = rand() % 100;) •Using cout...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

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