Answer:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
public class num4 {
public static void main(String[] args) {
int sum = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter number");
int num = in.nextInt();
ArrayList<Integer> list = new ArrayList<>();
while (num >= 0){
list.add(num);
System.out.println("Enter another positive number, enter a negative to stop");
num = in.nextInt();
}
int maximum = Collections.max(list);
int listSize = list.size();
for(int i:list){
sum = sum+i;
}
double average = (double) sum/listSize;
System.out.println("Maximum number entered is "+maximum);
System.out.println("Average of the number is "+average);
}
}
Explanation:
In this program:
- A Scanner class is used to receive user input and store in a variable
- A while loop with the condition while (num >= 0) is used to ensure only non-negative numbers are entered
- An ArrayList is created and used to store all the user's valid entries
- The Max Method in the Collections class is used to find the maximum value entered
- To find the average, a for loop is used to add all elements in the list and divide by the size of the list
- Finally the Average and maximum is printed out
Answer:
#include <iostream>
using namespace std;
void matrix(){
int row = 5, col = 6;
int myarr[row][col];
for (int i = 0; i < 5; i++){
for (int x = 0; x < 6; x++){
if (i == 0){
myarr[i][x] = (x+1)*(x+1);
}else if ( x == 0){
myarr[i][x] = (i+1)*(i+1)*(i+1);
} else if ( i == x){
myarr[i][x] = (i+1);
} else{
myarr[i][x] = myarr[i-1][x] + myarr[i][x-1];
}
}
}
for (int i = 0; i < 5; i++){
for (int x = 0; x < 6; x++){
cout<< myarr[i][x] << " ";
}
cout<< "\n";
}
}
int main(){
matrix();
}
Explanation:
The C++ source code defines a two-dimensional array that has a fixed row and column length. The array is a local variable of the function "matrix" and the void function is called in the main program to output the items of the array.
Answer:
advertisement should be nice and attractive to get attention of people