Programming 621 (C++) Exam Scope
Programming 621 (C++) Exam Scope
• IF..Else statements,
• For...& While Loops,
• Classes,
• Reading and Writing to Files
• Try and Catch exception
• Single Dimension Arrays
Pointer in C++: A pointer, is basically a variable that stores the memory address or location as its value. It
points to a data type (like int or string or char) of a similar type and is generated with the * operator. The
address of the variable one is working with is assigned to the pointer:
#include <iostream>
using namespace std;
//Swap function to swap 2 numbers
void swap(int *num1, int *num2)
{
int temp;
//Copy the value of num1 to some temp variable
temp = *num1;
//Copy the value of num2 to num1
*num1 = *num2;
//Copy the value of num1 stored in temp to num2
*num2 = temp;
}
int main()
{
int num1, num2;
//Inputting 2 numbers from user
cout << "\nEnter the first number : ";
cin >> num1;
cout << "\nEnter the Second number : ";
cin >> num2;
//Passing the addresses of num1 and num2
swap(&num1, &num2);
//Printing the swapped values of num1 and num2
cout << "\nFirst number : " << num1;
cout << "\nSecond number: " << num2;
return (0);
}
1 #include <iostream>
2 #include <cstdlib>
3
4 using namespace std;
5
6 int main() {
7 int num_count;
8 double sum, mean;
9
10 cout << "Enter the number of values: ";
11 cin >> num_count;
12
13 for (int i = 0; i < num_count; i++) {
14 double num;
15 cout << "Enter value " << i + 1 << ": ";
16 cin >> num;
17 sum += num;
18 }
19
20 mean = sum / num_count;
21 cout << "The mean value is: " << mean << endl;
22
23 return 0;
24 }
Write a C++ program to find the sum and average of one dimensional
integer array.
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,i,sum=0;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];
cout<<"\nThe sum of Array is :"<<sum;
cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Class - program
Write a program and input two integers in main and pass them to default constructor of
the class. Show the result of the addition of two numbers.
#include <iostream>
using namespace std;
class data{
public:
int nu1,nu2;
data(int numm1,int numm2){
nu1=numm1;nu2=numm2;cout <<"numbers initialized \n";
}
int sum_num(){return nu1+nu2;}
};
int num1;
int num2;
cout<<"Enter first number : ";
cin>>num1;
cout<<"Enter second number : ";
cin>>num2;
data set_nu(num1,num2);
return 0;
}
Writing to file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile;
// open an exist file fout.txt
outFile.open("number.txt", std::ios_base::app);
if (!outFile.is_open())
{ cout << " problem with opening the file ";}
else
{outFile <<"200 Sam"<<endl ;
cout << "done writing" <<endl;}
outFile.close();
}
Reading from file
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{//Declare and open a text file
ifstream INFile("C:/Users/Samuel/Documents/C++/numbers.txt");
string line;
int total=0;
while(! INFile.eof())
{
getline(INFile, line);
//converting line string to int
stringstream(line) >> total;
cout << line <<endl;
cout <<total +1<<endl;}
INFile.close(); // close the file
}