0% found this document useful (0 votes)
285 views6 pages

Programming 621 (C++) Exam Scope

The document discusses various C++ programming concepts: 1) It covers common programming structures like if-else statements, for and while loops, classes, and exception handling using try-catch blocks. 2) It provides code examples for swapping values using pointers, determining student grades, calculating averages, and handling exceptions. 3) It also demonstrates writing to and reading from files in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
285 views6 pages

Programming 621 (C++) Exam Scope

The document discusses various C++ programming concepts: 1) It covers common programming structures like if-else statements, for and while loops, classes, and exception handling using try-catch blocks. 2) It provides code examples for swapping values using pointers, determining student grades, calculating averages, and handling exceptions. 3) It also demonstrates writing to and reading from files in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

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:

1. Write a C++ code to swap values of two variables using pointers.

#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);
}

If… Else program


Write a C++ program that determines a student’s grade.The program will
read three types of scores (quiz, mid-term, and final scores) and determine
the grade based on the following rules:
if the average score =90% =>grade=A
-if the average score >= 70% and <90% => grade=B
-if the average score>=50% and <70% =>grade=C
-if the average score<50% =>grade=F
#include<iostream>
using namespace std;
int main()
{
float x;
float y;
float z;
float avg;
avg=x+y+z/3;
cout<<"enter first subject marks"<<endl;
cin>>x;
cout<<"enter second subject marks"<<endl;
cin>>y;
cout<<"enter third subject marks"<<endl;
cin>>z;
avg=x+y+z/3;
if (avg>=90)
{
cout<<"Result: grade is A"<<endl;
}
else if(avg>=70&&avg<90)
{
cout<<"Result: grade is B"<<endl;
}
else if(avg>=50&&avg<70)
{
cout<<"Result: grade is C"<<endl;
}
else
{
cout<<"Result: grade is F"<<endl;
}
}
Loops ( for and do-while ) programs
Here is a simple C++ program to find the mean value of a set of numbers:
C++

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 }

C++ Program Sum of odd Natural Number


1 #include<iostream>
2 #include <conio.h>
3 using namespace std;
4
5 int main()
6 {
7 int i=1;
8 int input;
9 int sum=0;
10
11
12
13 cout<<"plz Input number of terms : ";
14 cin>>input;
15 cout<<"The odd numbers are :";
16
17 do
18 {
19
20 cout<<"\t"<<2*i-1;
21 sum+=2*i-1;
22 i++;
23 }
24 while(i<=input);
25
26
27 cout<<endl;
28 cout<<"\nThe Sum of odd Natural Number upto "<<input<<" are:";
29 cout<<sum;
30 }
31

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 main (){

int num1;
int num2;
cout<<"Enter first number : ";
cin>>num1;
cout<<"Enter second number : ";
cin>>num2;

data set_nu(num1,num2);

cout << "The addition result on:"<< set_nu.sum_num()<<"\n";

return 0;
}

Try and Catch Program

• try - code that may raise an exception


• throw - throws an exception when an error is detected
• catch - code that handles the exception thrown by the throw keyword
// program to divide two numbers
// throws an exception when the divisor is 0
#include <iostream>
using namespace std;
int main() {
double numerator, denominator, divide;
cout << "Enter numerator: ";
cin >> numerator;
cout << "Enter denominator: ";
cin >> denominator;
try {
// throw an exception if denominator is 0
if (denominator == 0)
throw 0;
// not executed if denominator is 0
divide = numerator / denominator;
cout << numerator << " / " << denominator << " = " << divide << endl;
}
catch (int num_exception) {
cout << "Error: Cannot divide by " << num_exception << endl;
}

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
}

You might also like