Code a repetition control structure that:
Processes an array of integers in index order to sum its elements.
Only odd numbers should be included in the sum and processing should cease when either a negative integer or (be careful) the end of the array is encountered.
The array can be any length but you do not have to deal with an empty array.
Declare all required variables and give them initial values if required.
Display the output using myWindow.writeOutLine(…).
You do not need to code a method.
CODE:
public class Array {
public static void main(String[] args) {
int arr[] = { 2, 1, 8, 5, 6, 9, 1, 23, 6, 47, 8, 9, 1, -8, 6, 7, 5, 5, 7, 9, 9, 2, 1 };
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
sum += arr[i];
}
if (arr[i] < 0)
break;
}
// you need to make the Window class to run this
myWindow.writeOutLine(sum);
// normally System.out.println(); line is used to write data to console.
// System.out.println(sum);
}
}
OUTPUT:
Please upvote if you like my answer and comment below if you have any queries or need any further explanation.
Code a repetition control structure that: Processes an array of integers in index order to sum...
(+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100) to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0 (how many) Display the number of integers < 0 (how many) Display the...
Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index to the array? Shows the code. Consider the declaration: int v[1]; What is the index of the last element of this array? Declare an array named a of 10 int elements and initialize the elements (starting with the first) to the...
Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...
using matlab In Class Exercises 8-Arrays For the following exercises, assume an array is already populated, your job is to write a program that traverses the array. DO NOT HARD CODE the last index. In other words, you code for 1-4&6 should be able to handle the following 2 arrays: amp2 10 array1 [52, 63, 99, 71, 3.1] array2 - [99:-3: 45); For 5: wordArray1 wordArray2 ('number, 'arrays', 'indices', 'hello', finish' [number, 'different, 'finish? 1. Determine the sum of all...
Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 - 1000. You will pass the array into functions that will perform operations on the array. Function 1: Create a function that will output all the integers in the array. (Make sure output is clearly formatted. Function 2: Create a Function that will sum and output all the odd numbers in the array. Function 3: Create a Function that will sum and output all the...
Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...
Write a full C++program to do the following processes on a two dimension array of integers. Assume the size is 4x4 . 1- Fill the array with the numbers like this pattern: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 Note: Do not accept the values as input, or hard code it. Find the math to generate these numbers and fill the array with them. 2- Reset the main diagonal elements...
need question 3 assap using cin and cout outputs please Ctrl CSC 270 Final Exam-Spring2 1) [10 pts] Create anarray x which includes integers fron ltos obtain the array y which, even formula. Display both arrays x and y in a table with labeled column headings. 0 y- 2x+1 2) 120 pts] Consider the following 2D array. 01 2 -1 X-3 5 0 6 -3 7 -15 Use the standard notation and obtain the following (a) Create and display array...
I'm a little crunched for time and need some help with these first two questions. Thank you! Question 1.) Write a Java program that reads in sequence of integers from the user until user enters a negative number and stores them in an Integer ArrayList. Once read, display a bar chart based on the values stored in the ArrayList. For example, if the user inputs 2, 5, 3, 8, 4, and -1, then your program should display the bar chart...
Write a program that initializes an array with ten random integers and then prints out the following: Every element at an even index; Every even element All elements in reverse order; Only the first and last elements; The minimum and maximum element The sum of all elements The alternating sum of all elements, where the alternating sum contains all elements at even index added, and the elements at odd index subtracted. Please write comments above the piece of code that...