In: Computer Science
A)Write a C++ program using a while loop for Programming Exercise 5.1 on p. 193.
Turn in a printout of the program and a printout of the results.
Test the program for the two test cases in the book along with a third test case that includes 10 valid numbers (including some negative and some positive).
You have not shown the exact question of Ex5.1 I am still showing how to use while loop -
Consider we have array of 10 elements and we are using while loop to iterate the array and print all the positive elements present in the array
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
#include<bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = {10, -23, 50, 12, 54, -45, -54, 28,
26, -6};
int n = 10; //denotes number of elements
int i = 0; //denotes index number
cout<<"Positive elements present in
array are: ";
while(i<n) //Traverse all elements
{
if(arr[i]>0)
cout<<arr[i]<<" ";
i++;
//increment index by 1
}
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any