1)What is printed from the following statements: int x[] = int [10]; for(int i=5
ID: 3721252 • Letter: 1
Question
1)What is printed from the following statements:
int x[] = int [10];
for(int i=50; i<500; i +=50)
x[i] = i;
2)Create an array of 100 integers. Store random integers in the range 60-200 in the array elements. Write all the required statements, not the whole program.
3)Create a 2-dimensional array of strings with 2 rows and 3 columns. Assume each row has a person’s name, his/her city name, and its zip code. Write assignment statements to assign “Washington”, “Mt. Vernon”, “22309” to the second row elements of the array you created.
4)Write the method printArray() which accepts an array of decimal numbers and prints them in the method.
5)Write the method sumArray() which accepts an array of integers and returns the sum of all the numbers.
6)Write a method to accept a 2-D array of decimals and print the array in a matrix form.
7)Write statements to define an Animal class and its constructors. Each animal will have a species (like elephant) and classification (like mamal, fish, birds, reptiles, amphibians, and invertibrates).
8)Write statements to define an Elephant as a sub-class of Animal class and its constructors. Each elephant will have subspecies (Asian or African) and population (like 28000 Indian elephants).(Java)
Explanation / Answer
1.) The first code snippet is wrong, as the array must be initialized with a brace-enclosed initializer. Therefore, the first program fails to compile.
2.) //Code Begins(in C++)
#include <iostream>
using namespace std;
int main() {
int x[100];
for(int i = 0; i < 100; i++) {
x[i] = rand()%141 + 60; // generates random number between 60 to 200 both included.
}
return 0;
}
4.) The code goes something like this:-
void printArray(double arr[], int size) {
for(int i= 0; i< size; i++) {
cout<<arr[i]<<" ";
}
cout<<endl;
}
5.)
//Code Begin(in c++)
int sumArray(int arr[], int size) {
int ans = 0;
for(int i = 0; i < size; i++) {
ans += arr[i];
}
return ans;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.