Question

In: Computer Science

All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...

All in C++ programming language

1.

a.) convert for loop to while loop example
b.) convert while loop to for loop example

2.) pass one dimension array(and its size) to function example

Solutions

Expert Solution

1)

A)

CODE

#include<iostream>

using namespace std;

int main()
{
   //declaring variables
   int i,sum=0;
  
   //for loop for finding sum of intgers from 1 to 5
   for(i=1;i<=5;i++)
   {
       //calculating sum
       sum=sum+i;
   }
   //printing sum
   cout<<"Sum of 1-5 is "<<sum<<endl;
   sum=0;
   i=1;
  
   //while loop for finding sum of intgers from 1 to 5
   while(i<=5)
   {
       //calculating sum
       sum=sum+i;
      
       //incrementing value
       i++;
   }
   //printing sum
   cout<<"Sum of 1-5 is "<<sum<<endl;
   return 0;
}

OUTPUT

CODE SCREEN SHOT

B)

CODE

#include<iostream>

using namespace std;

int main()
{
   //declaring variables
   int i=1,fact=1;
  
   //while loop for finding factorial of 5
   while(i<=5)
   {
       //calculating factorial
       fact=fact*i;
      
       //incrementing value
       i++;
   }
   //printing factorial
   cout<<"Factorial of 5 is "<<fact<<endl;
   fact=1;
   i=1;
  
   //for loop for finding factorial 5
   for(i=1;i<=5;i++)
   {
       //calculating factorial
       fact=fact*i;
   }
   //printing factorial
   cout<<"Factorial of 5 is "<<fact<<endl;
   return 0;
}

OUTPUT

CODE SCREEN SHOT

2)

CODE

#include<iostream>

using namespace std;

//function for print an array
void printArray(int functionArray[],int size)
{
   //for loop
   for(int i=0;i<size;i++)
   {
       //printing array
       cout<<functionArray[i]<<endl;
   }
}

int main()
{
   //initializing an array
   int array[5]={1,2,3,4,5};
  
   //finding size of an array
   int size = sizeof(array)/sizeof(array[0]);
  
   //passing array and size to a function
   printArray(array,size);
   return 0;
}

OUTPUT

CODE SCREEN SHOT


Related Solutions

C programming. Explain by taking a programming example how do while loop is different from while...
C programming. Explain by taking a programming example how do while loop is different from while loop?
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
Class object in C++ programming language description about lesson inheritance example.
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson copy constructor example.
Class object in C++ programming language description about lesson copy constructor example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT