Answer:
#include<iostream>
using namespace std;
int main() {
cout<<"Enter The Size Of Array: ";
int size;
bool isBestCase=false;
cin>>size;
if(size<=0){
cout<<"Error: You entered an incorrect value of the array size!"<<endl;
return(0);
}
int array[size], key;
cout<<"Enter the numbers in the array, separated by a space, and press enter:";
// Taking Input In Array
for(int j=0;j<size;j++){
cin>>array[j];
}
//Your Entered Array Is
for(int a=0;a<size;a++){
cout<<"array[ "<<a<<" ] = ";
cout<<array[a]<<endl;
}
cout<<"Enter a number to search for in the array:";
cin>>key;
for(i=0;i<size;i++){
if(key==array[i]){
if(i==0){
isBestCase=true; // best case scenario when key found in 1st iteration
break;
}
}
}
if(i != size){
cout<<"Found value "<<key<<" at index "<<i<<", which took " <<++i<<" checks."<<endl;
} else{
cout<<"The value "<<key<<" was not found in array!"<<endl;
cout<<"We ran into the worst-case scenario!"; // worst-case scenario when key not found
}
if(isBestCase){
cout<<"We ran into the best case scenario!";
}
return 0;
}
Explanation:
The C++ source dynamically generates an array by prompting the user for the size of the array and fills the array with inputs from the user. A search term is used to determine the best and worst-case scenario of the created array and the index and search time is displayed.
write an algorithm and draw a flowchart to calculate the sum of of the first 10 natural numbers starting from 1
Answer:
#include <stdio.h>
void main()
{
int j, sum = 0;
printf("The first 10 natural number is :\n");
for (j = 1; j <= 10; j++)
{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}
Within a loop
Step 1: Initialize a variable to 1 (say x=1)
Step 2: Specifiy the condition
In this case x<=10
Step 3: Increment the variable by 1
using increment operator x++
OR
simply x+=1(x=x+1)
Step 4: Print the value of variable
Terminate the loop
Explanation:
Suppose that an intermixed sequence of push and pop operations are performed on a LIFO stack. The pushes push the numbers 0 through 9 in order; the pops print out the return value. For example"push(0); pop(); push(1)" and "push(0); push(1); pop()" are possible, but "push(1); push(0); pop()" is not possible, because 1 and 0 are out of order. Which of the following output sequence(s) could occur? Select all that are possible.
a) 12543 609 8 7 0
b) 01564 37928
c) 6543210789
d) 02416 75983
e) 46 8 75 32 901
Answer:
b) 01564 37928
e) 26 8 75 32 901
Explanation:
Pushes and pulls are the computer operations which enable the user to perform numerical tasks. The input commands are interpreted by the computer software and these tasks are converted into numeric values to generate output.
What is the total March Expenditure now? (cell Q2)
#include
main()
{
int i, n=10, rem, inc=0;
scanf("%d",&n);
for(i=1; i =3)
inc++;
else;
inc--;
}
printf("Final Value=\n",inc);
}
SOLVE THIS PROGRAM...
Answer:inpossible !889
Explanation: I npossible !889
For which of the following tasks would a for-each loop be appropriate?
A. Reversing the order of the elements in an array.
B. Printing all the elements at even indices of an array.
C. Printing every element in an array.
D. Determining how many elements in an array of doubles are positive.
E. Determining whether an array of Strings is arranged in alphabetical order.
F. Printing every even number in an array of ints.
Answer:
The answer is "Option B, C, and F".
Explanation:
In the For-each loop, it is used for traversing items in a sequence is the control flow expression. It typically required a loop becomes referred to it as an enhanced loop for iterating its array as well as the collection elements. This loop is a version shortcut that skips the need for the hasNext() method to get iterator and the loop and over an iterator, and the incorrect choice can be determined as follows:
In choice A, it is wrong because it can't reverse the array element.In choice D, it is wrong because it can't determine the array of positive.In choice E, it is wrong because it can't determine an array of the sting in alphabetical order.