C++ Solutions
C++ Solutions
C++ Solutions
Write a code that output " Hello I am Silas .A. Silas a student in CNET232 and
My student no is 300570842. declare a constant variable, int variable, float var
iable,
character variable. Then input the number of gallons and let litres be "gallon
* GALLONSTOLITRES".
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain()
{
const float constantvariable=10.0;
const float GALLONSTOLITRES=3.79;
int integervariable=100;
double floatvariable=3.14;
double gallons=0.0;
double litres=0.0;
char charactervariable='V';
cout << "Hello I am Silas .A. Silas a student in CNET232.\n";
cout << "My student ID is 300570842.\n";
cout << "\n";
cout << "My Constant Variable is " << constantvariable <<"\n";
cout << "My Integer Variable is " << integervariable <<"\n";
cout << "My Float Variable is " << floatvariable <<"\n";
cout << "My Character Variable is " << charactervariable <<"\n";
cout << "Please input the number of gallons\n";
cin >> gallons; //Gets input from
User
litres=gallons*GALLONSTOLITRES; //Gallons to Litr
es conversion factor
cout << "The number of litres is " << litres <<"\n"; //Outputs the number
of litres to screen
return 0;
}
ASSIGNMENT 2:
Asks the user for the number of peas in a pod.Asks the user for the number of po
ds collected.Prints out to the screen
the number of peas collected. Input the price of a pod of peas in dollars. Calcu
late the price of a single pea in dollars
based on the pod price.Output these values to the screen.Write a program that pr
ompts the user for a number that can have decimals and raises this number
to the power of 4 ( for example 2 raised to the power of 4 = 16). The program s
hould then output this value to the screen.
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
int _tmain()
{
int NumberOfPeasInPod;
int NumberOfPods;
int NumberOfPeas;
float Number, Answer;
float PriceOfPod, PriceOfPea;
cout << "Enter no of peas in pods = ";
cin >> NumberOfPeasInPod;"\n\n";
cout << "Enter no of Pods= ";
cin >> NumberOfPods; "\n\n";
NumberOfPeas = NumberOfPeasInPod * NumberOfPods;
cout << "Number of Peas are = " << NumberOfPeas << "\n\n";
cout << "Enter the price of a pod of pea = $";
cin >> PriceOfPod; "\n";
PriceOfPea = PriceOfPod / NumberOfPeasInPod;
cout << "The price of each pea in the pod is " << PriceOfPod<< "/" <<Num
berOfPeasInPod << " = $" << PriceOfPea << "\n\n";
cout << "Enter a number to raise to the power of 4 = ";
cin >> Number; "\n\n";
Answer = pow(Number,4);
cout << "Your number "<< Number << "^4 = " << Answer << "\n\n";//Your nu
mber 2.6 ^4 = .....//
return 0;
}
QUESTION 1:
Write a code that will multiply some numbers by 2 if the number is between 1 an
d 100 (including 1 or 100) and
if it is evenly divisible by 3; otherwise, multiply by 3 if it is not divisible
by 3, finally, if it isn't between 1 and
100, multiply the number by the number modulus 100.
#include "stdafx.h"
#include <math.h>
#include <stdio.h>
#include <iostream>
using std::cin;
using std::cout;
using namespace std;
int _tmain()
{
int a,b;
cout << "THE RULES:\n";
cout << "Rule 1: If no is btw 1 & 100, and divisible by 3...multiply by
2\n";
cout << "Rule 2: If no is btw 1 & 100, and not divisible by 3...multiply
by 3\n";
cout << "Rule 3: If no is not btw 1 & 100, find the no modulus 100\n";
cout << "\nEnter any no btw. 1 and 100 = ";
cin >> a; "\n";
if (a >= 1 && a<=100 && a % 3 == 0) // "&&" means AND //
{
b = a * 2;
cout << "Your answer matches Rule 1: " << b << "\n";
}
else if (a >= 1 && a<=100 && a % 3 != 0) // "!" means not equals
to //
{
b = a * 3;
cout << "Your answer matches Rule 2: = " << b << "\n";
}
else if (a <= 0 || a > 100) // "||"
means OR //
{
b = a * (a % 100);
cout << " Your answer matches Rule 3: = " << b << "\n";
}
return 0;
}
ASSIGNMENT 3 - TASK 1:
/* math.cpp - Simple math function demonstration. */
#include "stdafx.h"
#include <iostream>
#include <climits>
#include <cfloat>
#include <cmath>
using namespace std;
// Declare a function named 'sphere' that returns
// a double and expects a double for input.
double sphere(double rad);
const double PI = 3.14;
int main()
{
// Define some variables
char char_val = 'a';
int int_val = 0xff;
float float_val = 11.1;
double double_val = 66.123456789;
cout << "char_val = " << char_val << endl;
cout << "int_val = " << int_val << endl;
cout << "float_val = " << float_val << endl;
cout << "double_val = " << double_val << endl;
// More types, with modifiers
signed char schar_val = -98;
unsigned char uchar_val = 97;
short short_min = SHRT_MIN;
short short_max = SHRT_MAX;
long long_min = LONG_MIN;
long long_max = LONG_MAX;
cout << "schar_val = " << schar_val << endl;
cout << "schar_val = " << (int) schar_val << endl;
cout << "uchar_val = " << hex << uchar_val << endl;
cout << "short_min = " << short_min << endl;
cout << "short_max = " << short_max << endl;
cout << "long_min = " << dec << long_min << endl;
cout << "long_max = " << long_max << endl;
// Some floating point constant
cout << "FLT_MIN = " << FLT_MIN << endl;
cout << "FLT_MAX = " << FLT_MAX << endl;
cout << "DBL_DIG = " << DBL_DIG << endl;
cout << "LDBL_EPSILON = " << LDBL_EPSILON << endl;
TASK 2:
/* math.cpp - Simple math function demonstration. */
// using precision and setprecision
#include <StdAfx.h>
#include <iostream>
//using namespace std;
#include <iomanip>
using namespace std;
// using C++ wrappers to access C function
#include <math.h>
int main(void)
{
double theroot = sqrt(11.55);
cout<<"Square root of 11.55 with various"<<endl;
cout<<" precisions"<<endl;
cout<<"---------------------------------"<<endl;
cout<<"Using 'precision':"<<endl;
for(int point=0; point<=8; point++)
{
cout.precision(point);
cout<<theroot<<endl;
}
cout<<"\nUsing 'setprecision':"<<endl;
for(int place=0; place<=8; place++)
{
cout<<setprecision(place)<<theroot<<endl;
}
return (0);
}
TASK 3:Using if and else create a constant salary and multiply it by no of hours
. if hours is less or equal to 40, and multiply by 1.5 for any no of hours
greater than 40, and add it to salary for 40 hrs.
// Assign01.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
int _tmain()
{
char str[25]; //command to enter letters up to 15 in l
ength
float hours;
const int SALARY = 2;
float salary, mainSalary, overTime;
cout << "What is your name? ";
gets (str); "\n"; // instead of "cin >>" use "gets (str);.
...it can collect space e.g. Silas Aham
cout << "How many hours or/and half hour have you worked in last week? "
;
cin >> hours; "\n";
cout << "Work per hour is $" << SALARY << " if less than 40hrs" << "\n";
if (hours <= 40)
{
salary = SALARY * hours;
cout << str << " made $" << salary << " last week"<< "\n";
}
else (hours > 40); // if you use "else if" you wont need to add ";" e.g
. else if (hours > 40)
{
mainSalary = SALARY * 40;
overTime = ((hours - 40)*(1.5* SALARY));
salary = mainSalary + overTime;
cout << str << " made $" << salary << " last week"<< "\n";
}
return 0;
}
TASK 4: Enter three integer and compute the squared and squaroot using a setprec
ision of 4 decimal for squared and 2 decimal for square root.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
// Declaration statements
double num1, num2, num3;
// print your name, student number and date
cout << "Name: Silas .A. Silas\n";
cout << "Student Number: 300570842" << endl;
cout << "Date: 10th June, 2010\n" << endl;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
cout << endl;
cout << "Number Squared Square root" << endl;
cout.precision(12);
cout << fixed << setw(6) << setprecision(2) << num1;
cout << setw(12) << setprecision(4) << num1*num1;
cout << setw(14) << setprecision(2) << sqrt(num1) << endl;
cout << fixed << setw(6) << setprecision(2) << num2;
cout << setw(12) << setprecision(4) << num2*num2;
cout << setw(14) << setprecision(2) << sqrt(num2) << endl;
cout << fixed << setw(6) << setprecision(2) << num3;
cout << setw(12) << setprecision(4) << num3*num3;
cout << setw(14) << setprecision(2) << sqrt(num3) << endl;
TASK 5:
#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// Declaration statements
const int FREEZE_POINT=32;
double user_temp, celsius_temp;
char unit;
// print your name, student number and date
cout << "Name: Silas .A. Silas\n";
cout << "Student Number: 300570842\n";
cout << "Date: 10th June, 2010\n" << endl;
cout << endl;
cout << "Enter water temperature: ";
cin >> user_temp;
cout << "Enter C (for Celsius) or F (for Farenheit): ";
cin >> unit;
if (unit != 'F' && unit != 'C')
cout << "Please C or F" << endl;
else
{
if (unit == 'F')
celsius_temp = (user_temp - FREEZE_POINT) * 5 / 9;
else
celsius_temp = user_temp;
cout << fixed << setprecision(4);
cout << "Water temperature is " << setprecision(1)<< celsius_tem
p << " Celsius" << endl;
if (celsius_temp == 0)
cout << "Water starts to freeze at this temperature" <<
endl;
if (celsius_temp == 100)
cout << "Water starts to boil at this temperature" << en
dl;
if (celsius_temp < 0)
cout << "Water is a solid at this temperature" << endl;
if (celsius_temp > 100)
cout << "Water is a gas at this temperature" << endl;
if (celsius_temp > 0 && celsius_temp < 100)
cout << "Water is a liquid at this temperature" << endl;
}
cout << endl;
system("pause");
return 0;
}
TASK 6: Create a while loop to ask user to enter an integer btw. 1 & 10, continu
e to loop until if the value isn't btw 1 & 10
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num=1;
cout <<"Enter a number btw. (1-10): ";
cin >> num; "\n";
if (num <=10)
num=num;
}
cout <<"The number you have entered is " << num << endl;
cout <<"Thanks for entering valid number\n";
return 0;
}
TASK 7: Input a number btw 1 & 255 and convert the value to binary number
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void binary(int);
void main(void) {
int number;
cout <<"\t\t SILAS AHAMEFULAS SILAS...300570842\n\n";
cout << "Enter a number between 1 and 255: ";
cin >> number;
if(number < 0 || number > 255)
cout << "That is not between 1 and 255.\n";
else {
cout << number << " converted to binary is: ";
binary(number);
cout << endl;
}
}
void binary(int number) {
int remainder;
while(number <= 1) {
cout << number;
return;
}
remainder = number%2;
binary(number >> 1);
cout << remainder;
}
TASK 10:
#include "stdafx.h"
#include<iostream>
using namespace std;
int main(int argc, _TCHAR* argv[])
{
const int SIZE = 10;
int numbers[SIZE];
int counter=0;
int temp_swap=0;
cout << "This program sorts 10 integers in descending order" << endl <<
endl;
cout << "Please enter 10 integers to be sorted:" << endl << endl;
while(counter<SIZE)
{
cout << counter+1 << "). Enter a number: ";
cin >> numbers[counter++];
}
TASK 11.
#include "stdafx.h"
#include<iostream>
#include <iomanip>
#include <cmath>
using namespace std;
void main()
{
const int SIZE=10;
const int passmark=50;
int markArray[SIZE]; // Declare and initialize an array of integers call
ed markArray (2 marks)
int countpass=0; // Declare 2 integer variables to keep track of
the count of passing
int countfail=0; // and failing students (1 mark)
int counter=0;
double average=0.0;
double sum=0.0;
cout << "Name: SILAS AHAMEFULA SILAS \n";
cout << "Student Number: 300570842 \n";
cout << "Date: 8th July, 2010\n" << endl;
cout << "Please enter 10 Student marks:" << endl << endl;
while(counter<SIZE)
{
cout << "Student " << counter+1 << " mark: ";
cin >> markArray[counter++];
}
for(counter=0; counter<SIZE; counter++)
{
if( markArray[counter] >= passmark )
{
countpass++;
}
else
countfail++;
}
for (counter = 0; counter < SIZE; ++counter)
{
sum+=markArray[counter];
average = sum/SIZE;
}
cout <<"\n";
cout<<"The class average was: "<<average<<"\n";
cout <<countpass<< " students passed the exam\n";
cout <<countfail<< " students failed the exam\n";
system("pause");
}
TASK 12:
#include "stdafx.h"
#include<iostream>
using namespace std;
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2);
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2);
void DisplayFraction(int &Num, int &Denom);
int main()
{
char an;
int Num, Denom, Num2, Denom2;
do
{
ReadFraction (Num, Denom, Num2, Denom2);
AddFraction(Num, Denom, Num2, Denom2);
DisplayFraction(Num, Denom);
cout << "\nWould you like to do another fraction? " ;
cin >> an;
cout << "\n";
} while ((an == 'y') || (an == 'Y'));
return(0);
}
void ReadFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
cout << "Enter the numerator for the first fraction: ";
cin >> Num; "\n";
do
{
cout << "Enter the denominator for the first fraction: ";
cin >> Denom;
}
while (Denom==0);
cout << "\n";
cout << "Enter the numerator for the second fraction: ";
cin >> Num2; "\n";
do
{
cout << "Enter the denominator for the second fraction: ";
cin >> Denom2;
}
while (Denom2==0);
cout << "\n";
}
void AddFraction(int &Num, int &Denom, int &Num2, int &Denom2)
{
Num=(Num * Denom2) + (Denom * Num2);
Denom=(Denom * Denom2);
}
void DisplayFraction(int &Num, int &Denom)
{
cout << "The added fraction is "<<Num << "/" <<Denom<< endl;
}
TASK 13:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream input_stream;
char input_file_name[16], output_file_name[16];
cout << "Enter the input file name (maximum 15 characters):\n";
cin >> input_file_name;
cout << "Enter the output file name (maximum 15 characters):\n";
cin >> output_file_name;
input_stream.open(input_file_name);
output_stream.open(output_file_name);
TASK 14:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
int numStudents;
char names[10][40];
int ages[10];
cout << "Please enter the number of students in the class: ";
cin >> numStudents;
while( cin.get() != '\n' )
;
while( numStudents < 0 || numStudents > 10 )
{
cout << "Only 10 students allowed. Please try again..." << endl
;
cout << "Enter the number of students: ";
cin >> numStudents;
while( cin.get() != '\n' )
; .
}
int nextStudent = 0;
do
{
cout << "Please enter the student's name: ";
cin.getline( names[nextStudent], sizeof(names[nextStudent]) );
cout << "Please enter the student's age: ";
cin >> ages[nextStudent];
while( cin.get() != '\n' )
;
nextStudent++;
}
while( nextStudent < numStudents );
cout << "Student Roster" << endl;
cout << "--------------" << endl;
char output_file_name[16];
ofstream output_stream;
cout << "Enter the output file name (maximum 15 characters):\n";
cin >> output_file_name;
output_stream.open (output_file_name);
for( int i = 0; i < numStudents; i++ )
{
cout << names[i] << " is " << ages[i] << " years old." << endl;
output_stream << names[i] << " is " << ages[i] << " years old. \
n";
}
cout << endl;
return 0;
}
TASK:15:
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
void get_choice(int &pick_number);
void process_choice(int pick_number);
void add_record(const char record_file[]);
void list_record(const char record_file[]);
// Student Record structure goes here.
struct student_info
{
char name[22];
char id[9];
char email[22];
};
int main()
{
int pick_number = 1;
do
{
get_choice(pick_number);
process_choice(pick_number);
}while (pick_number);
return 0;
}
// Add your code here to make your choice.
void get_choice(int &choice)
{
do {
cout << "Please enter choice:" << endl;
cout << " 0 - Exit" << endl;
cout << " 1 - Add a record" << endl;
cout << " 2 - List all records" << endl;
cin >> choice;
} while (choice != 0 && choice != 1 && choice != 2);
cin.ignore(256, '\n');
};
void process_choice(int choice)
{
if (choice ==1) add_record("data.txt");
else if(choice == 2) list_record("data.txt");
else if (choice == 0) cout << "You have chosen to exit the program." <<
endl;
else cout << "Wrong choice, program aborted." << endl;
}
// Put your add record code here.
void add_record(const char record_file[])
{
ofstream outfile;
student_info list;
cout << "Pls enter name: ";
cin.getline(list.name, 21);
cout << "Pls enter id: ";
cin >> list.id;
cout << "Pls enter email: ";
cin >> list.email;
//ios::app - opening file for appending
outfile.open(record_file, ios::app);
if(outfile){ // If no error occurred while opening file
outfile << list.name << '\t';
outfile << list.id << '\t';
outfile << list.email << '\n';
}
else
cout << "Error...." << endl;
outfile.close();
}
// Put your list record code here.
void list_record(const char record_file[])
{
ifstream infile;
char temp[80];
//opening file to read
infile.open(record_file);
infile.getline(temp,80);
while(!infile.eof()){
cout << temp << endl;
infile.getline(temp,80);
}
infile.close();
}
// Structures01.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;
void get_choice(int &pick_number);
void process_choice(int pick_number);
void add_record(const char record_file[]);
void list_record(const char record_file[]);
// Student Record structure goes here.
struct student_info
{
char name[22];
char id[9];
char email[22];
};
int main()
{
int pick_number = 1;
do
{
get_choice(pick_number);
process_choice(pick_number);
}while (pick_number);
return 0;
}
// Add your code here to make your choice.
void get_choice(int &choice)
{
do {
cout << "Please enter choice:" << endl;
cout << " 0 - Exit" << endl;
cout << " 1 - Add a record" << endl;
cout << " 2 - List all records" << endl;
cin >> choice;
} while (choice != 0 && choice != 1 && choice != 2);
cin.ignore(256, '\n');
};
void process_choice(int choice)
{
if (choice ==1) add_record("data.txt");
else if(choice == 2) list_record("data.txt");
else if (choice == 0) cout << "You have chosen to exit the program." <<
endl;
else cout << "Wrong choice, program aborted." << endl;
}
// Put your add record code here.
void add_record(const char record_file[])
{
ofstream outfile;
student_info list;
cout << "Pls enter name: ";
cin.getline(list.name, 21);
cout << "Pls enter id: ";
cin >> list.id;
cout << "Pls enter email: ";
cin >> list.email;
//ios::app - opening file for appending
outfile.open(record_file, ios::app);
if(outfile){ // If no error occurred while opening file
outfile << list.name << '\t';
outfile << list.id << '\t';
outfile << list.email << '\n';
}
else
cout << "Error...." << endl;
outfile.close();
}
// Put your list record code here.
void list_record(const char record_file[])
{
ifstream infile;
char temp[80];
//opening file to read
infile.open(record_file);
infile.getline(temp,80);
while(!infile.eof()){
cout << temp << endl;
infile.getline(temp,80);
}
infile.close();
}