QUESTION 1 What’s the difference between an entry-condition loop and an exit-con
ID: 667480 • Letter: Q
Question
QUESTION 1
What’s the difference between an entry-condition loop and an exit-condition loop? Which kind is each of the C loops? Provide a detailed response with examples.
QUESTION 2
Write a for loop that prints the values 1 2 4 8 16 32 64 by increasing the value of a counting variable by a factor of two in each cycle.
QUESTION 3
Write a function that takes three arguments: a pointer to the first element of a range in an array, a pointer to the element following the end of a range in an array, and an int value. Have the function set each element of the array to the int value.
QUESTION 4
Define a class to represent a bank account. Data members should include the depositor’s name, the account number (use a string), and the balance. Member functions should allow the following by showing the class declaration only:
Creating an object and initializing it.
Displaying the depositor’s name, account number, and balance
Depositing an amount of money given by an argument
Withdrawing an amount of money given by an argument
Explanation / Answer
Answer 1:
The entry condition loop checks the condition first and then execute the loop body.
Example: while loop, for loop.
The exit condition loop first executes the loop body and then check the condition.
Example: do while loop.
Therefore, for the exit condition loop, the body of the loop will run at-least once.
Since, the condition is checked after the execution of the loop body.
while (check condition)
{
Loop body;
}
for (init; check condition; increment or decrement)
{
Loop body;
}
do
{
Loop body;
} while (check condition);
Code:
//Include the header file.
#include <stdio.h>
//Begin the main() function.
int main()
{
//Declare the variables.
int j = 0, w = 0, count = 10;
//Example of do while loop.
printf("Case1: ");
do
{
//Print the value of j.
printf("%d ", j);
//Increase the value of j by 1.
j++;
} while (j<count);
//Example of while loop.
while (w < 10)
{
//Print the value of w.
printf("%d ", w);
//Increase the value of w.
w++;
}
//Example of for loop.
for (int a = 0; a < 5; a++)
{
//Print the value of i.
printf("%d ", a);
}
//Return the value for the main() function.
return 0;
}
Answer 2:
//Begin the for loop.
for (int num = 1; num <= 64; num *= 2)
{
//Display the values.
cout << num << " ";
}
Answer 3:
//Define a function with parameters.
void funct(int *start_index, int *last_index, int v)
{
//Start the while loop.
while (*start_index != *last_index)
{
//Update the values of the array with
//the int value.
*start_index = v;
//Increase the pointer.
start_index++;
}
}
Answer 4:
//Include the header file for visual studio.
#include "stdafx.h"
//Include the header files.
#include<iostream>
#include<string>
using namespace std;
//Create a class bank.
class bank_account
{
//Declare the variables.
string dipositor_name;
string account_number;
int Account_balance;
//Define the access specifier.
public:
//Create a parameterized constructor.
bank_account(string d_name, string account_num,
int bal)
{
//Assign the value to the private members.
dipositor_name = d_name;
account_number = account_num;
Account_balance = bal;
}
//Define a function deposit() with a parameter
//amount.
void Amount_to_deposit(int amount)
{
//Add the amount to the initial balance.
Account_balance = Account_balance + amount;
}
//Define a function withdrawl() with a parameter
//amount.
void withdrawl_from_bank(int amount)
{
//Check the amount with the balance.
if (Account_balance > amount)
{
//Substract the withdrawl amount
//from the balance.
Account_balance = Account_balance - amount;
}
//Else part.
else
{
//Print the statement.
cout << "Enter the valid amount." << endl;
}
}
//Define the function displayAccountinfo().
void Account_information()
{
//Display the name of the depositor, AccountNo
//and the Balance.
cout << "Depositor's Name: " << dipositor_name
<< endl;
cout << "Account Number: " << account_number
<< endl;
cout << "Total Balance: " << Account_balance
<< endl;
}
};
//Define the main() function.
int main()
{
//Create a parameterized object of class bank.
bank_account j("Jony_Dep", "123", 1000);
//Call the function deposit() by passing the amount
//as a parameter.
j.Amount_to_deposit(1000);
//Call the function withdrawl().
j.withdrawl_from_bank(500);
//Call the function displayAccountinfo();
j.Account_information();
//Use the statement to pause the screen in visual
//studio.
system("pause");
}
The complete code for the question 2 is as follows:
//Include the header file for visual studio.
#include "stdafx.h"
//Include the header files.
#include<iostream>
using namespace std;
//Define the main() function.
int main()
{
//Begin the for loop.
for (int num = 1; num <= 64; num *= 2)
{
//Display the values.
cout << num << " ";
}
//Use the statement to pause the screen in visual
//studio.
system("pause");
}
The complete code for the question 3 is as follows:
//Include the header file for visual studio.
#include "stdafx.h"
//Include the header file.
#include<iostream>
using namespace std;
//Create a class test.
class test
{
//Define the access specifier.
public:
//Create a default constructor.
test()
{
}
//Define a function with parameters.
void funct(int *start_index, int *last_index, int v)
{
//Start the while loop.
while (*start_index != *last_index)
{
//Update the values of the array with
//the int value.
*start_index = v;
//Increase the pointer.
start_index++;
}
}
};
//Define the main() function.
int main()
{
//Declare the variables f type int.
int *array;
int x, y, value, size, *start, *end;
//Create an object of test class.
test t;
//Prompt the user to input the size of the array.
cout << "Enter the size of the array : " << endl;
cin >> size;
array = new int(size);
//Prompt the user to enter the elements of the array.
cout << "Enter the elements of the array : " << endl;
//Begin the for loop.
for (int i = 0; i < size; i++)
{
cin >> array[i];
}
//Prompt the user to enter the starting index
//of the range.
cout << "Enter the starting index of the array : "
<< endl;
cin >> x;
//Prompt the user to enter the last index
//of the range.
cout << "Enter the last index of the range : "
<< endl;
cin >> y;
//Prompt the user for int value to update the array
//values with the int value.
cout << "Enter the int value : "
<< endl;
cin >> value;
//Start contain the address of the starting
//index of the range.
start = &array[x];
//end contain the address of the last
//index of the range.
end = &array[y + 1];
//Call the function.
t.funct(start, end, value);
//Begin the for loop.
for (int i = 0; i < size; i++)
{
//Display the updated array.
cout << array[i] << endl;
}
//Use the statement to pause the screen in visual
//studio.
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.