Revision of Programming Fundamentals and Structured Programming Concepts
Revision of Programming Fundamentals and Structured Programming Concepts
Programming Concepts
Objective:
Revision of concepts that you’ve already discussed and implemented in Programming Fundamentals
and Structured Programming.
1D array
Sample Lab Task 1:
Write a program in C++ which implements the bubble sort algorithm. It should sort the input array in ascending
order.
Sample Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10], i,j,temp;
cout << "Enter the elements of array: " << endl;
for (i = 0; i<10; i++){
cin >> arr[i];
}
cout << "The elements of array before sorting are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
for (i = 0; i < 10; i++){
for (j = 0; j < 9; j++){
if (arr[j + 1] < arr[j]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "\nThe elements of array after sorting in ascending order are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
cout << endl;
}
2D array
Sample Lab Task 2:
Write a program in C++ which declares a 2D array of size 3 by 5, take input from the user in 2D array and
show the output in matrix form.
Sample Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
char array
Sample Lab Task 3:
Write a C++ program to take user name as input and print his/her name in reverse order. For example, reverse of
Ahmed Ali is ilA demhA.
Sample Code:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main(){
char name[30];
cout << "Please enter your name: ";
gets_s(name);
cout << "My name before reverse order is : ";
puts(name);
cout << "\nMy name after reverse order is : ";
for (int i = strlen(name) - 1; i >= 0; i--){
cout << name[i];
}
cout << endl;
}
Functions (Pass By Value)
Sample Lab Task 4:
Write a function "check_prime()" to check whether a number is prime or not in the given intervals.
Sample Code:
Write a function which takes an octal number from user and give its output in decimal.
Sample Code:
#include "stdafx.h"
#include <iostream>
#include<math.h>
using namespace std;
int oct2dec(int oct)
{
int r, i = 0, s = 0;
while (oct != 0)
{
r = oct % 10;
s = s + r*(int)pow(8, i);
oct = oct / 10;
i++;
}
return s;
}
void main()
{
int n, num = 0;
Sample Code:
#include <iostream>
#include <math.h> // for sin() and cos()
int main()
{
double dSin = 0.0;
double dCos = 0.0;
// GetSinCos will return the sin and cos in dSin and dCos
GetSinCos(30.0, dSin, dCos);
Write a function "bubblesort" which takes two arguments array itself & size and sort the array using bubble sort
algorithm in descending order.
Sample Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubblesort(int arr[], int size){
int i, j, temp;
for (i = 0; i<size; i++){
for (j = 0; j<size - 1; j++){
if (arr[j + 1]>arr[j]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10], i,j,temp;
cout << "Enter the elements of array: " << endl;
for (i = 0; i<10; i++){
cin >> arr[i];
}
cout << "The elements of array before sorting are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
bubblesort(arr, 10); //Passing an array to function
cout << "\nThe elements of array after sorting in descending order are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
cout << endl;
}
Write a program to dynamically allocate memory that takes the size of array entered by the user. Take input
from the user in array and output the number of elements entered by the user. Also de-allocate the used memory.
Sample Code:
#include <iostream>
using namespace std;
int main()
{
int i, n;
int * p;
cout << "Enter the size of array ? ";
cin >> i;
p = new int[i]; // memory allocation
if (p == NULL)
cout << "Error: memory could not be allocated";
else
{
for (n = 0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n = 0; n<i; n++)
cout << p[n] << ", ";
delete[] p; // memory deallocation
}
cout << endl;
return 0;
}
Sample Lab Task 9:
Write a function void find_max_min(int *p,int n ,int* max, int* min); that will find max and min value from
an array of size n.
Sample Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
void find_max_min(int *p, int n, int *max, int *min){
int i;
for (i = 0; i < n; i++)
{
if (*max <= *p)
*max = *p;
if (*min >= *p)
*min = *p;
p++;
}
}
void main()
{
int n, i, max,min;
int *p;
cout << "Enter the size of array: ";
cin >> n;
p = new int[n]; // memory allocation
cout << "Enter " << n << " elements in the array: \n";
for (i = 0; i < n; i++)
cin >> p[i];
cout << "Elements in the array are:\n";
for (i = 0; i < n; i++){
cout << p[i] << " ";
}
cout << endl;
max = p[0];
min = p[0];
find_max_min(p, n, &max, &min);
cout << "\nMinimum element in the array is: " << min;
cout << "\nMaximum element in the array is: " << max;
delete[] p; // memory deallocation
cout << endl<<endl;
}
(Functions)
Task 3:
Write a C++ function power () which takes two integers x and y as parameters and returns the
value of xy. The prototype of your function must be int power (int, int). Write appropriate main
function to test the power function.
Task 4:
Write a C++ function power () which takes addresses of two integers x and y as parameters.
The prototype of your function must be void power (int* x, int* y). Write appropriate main function to
test the power function.
Before implementing this function, try it out on a small array of size 5. Plan it properly on a piece of
paper and then continue.
It should sort the input array in ascending order.
(Pointers)
Task 6:
Complete the program by filling in the code. (Areas in bold) This problem requires that you study very
carefully the code already written to prepare you to complete the program.
// This program demonstrates the use of dynamic variables
#include <iostream>
using namespace std;
int main()
{
int pos;
char * name;
int * one;
int * two;
int * three;
int result;
//echo print
cout<< "The three numbers are " <<endl;