0% found this document useful (0 votes)
28 views8 pages

Revision of Programming Fundamentals and Structured Programming Concepts

The document provides sample code and tasks to review concepts related to arrays, functions, and pointers in C++. It includes examples of sorting 1D and 2D arrays, passing arrays and values to functions, dynamically allocating and deallocating memory using pointers, and finding maximum and minimum elements in an array using a pointer function. Sample tasks are provided for writing functions to check if a number is prime, convert between octal and decimal, calculate sine and cosine values passed by reference, and sort an array using bubble or insertion sort.

Uploaded by

alielina53
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
28 views8 pages

Revision of Programming Fundamentals and Structured Programming Concepts

The document provides sample code and tasks to review concepts related to arrays, functions, and pointers in C++. It includes examples of sorting 1D and 2D arrays, passing arrays and values to functions, dynamically allocating and deallocating memory using pointers, and finding maximum and minimum elements in an array using a pointer function. Sample tasks are provided for writing functions to check if a number is prime, convert between octal and decimal, calculate sine and cosine values passed by reference, and sort an array using bubble or insertion sort.

Uploaded by

alielina53
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 8

Revision of Programming Fundamentals and Structured

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>

using namespace std;


void main()
{
int disp[3][5];
int i, j;
cout<<"Enter elements of array: "<<endl;
for(i=0; i<3; i++)
{
for(j=0;j<5;j++)
{
cin>>disp[i][j];
}
}
cout<<"The elements of array are: "<<endl<<endl;
for(i=0;i<3;i++){
for(j=0;j<5;j++){
cout<<setw(4)<<disp[i][j];
}
cout<<endl;
}
cout<<endl;
}

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:

int check_prime(int num);


int main(){
int n1, n2, i, flag;
cout << "Enter two numbers(intervals): ";
cin >> n1 >> n2;
cout << "Prime numbers between " << n1 << " and " << n2<<" are ";
for (i = n1 + 1; i<n2; i++)
{
flag = check_prime(i); //Passing a variable to function
if (flag == 0)
cout << i<<" ";
}
cout << endl;
return 0;
}

int check_prime(int num) /* User-defined function to check prime number*/


{
int j, flag = 0;
for (j = 2; j <= num / 2; j++){
if (num%j == 0){
flag = 1;
break;
}
}
return flag;
}

Sample Lab Task 5:

Write a function which takes an octal number from user and give its output in decimal.

int oct2dec(int oct) //Function Prototype

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;

cout << "Please Enter octal number: ";


cin >> n;
num = oct2dec(n);
cout << "The equivalent number in decimal is : " << num;
cout << endl;
}

Functions (Pass By Reference)


Sample Lab Task 6:

Write a function that calculates Sin and Cos values of an integer.

Sample Code:

#include <iostream>
#include <math.h> // for sin() and cos()

void GetSinCos(double dX, double &dSin, double &dCos)


{
dSin = sin(dX);
dCos = cos(dX);
}

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

std::cout << "The sin is " << dSin << std::endl;


std::cout << "The cos is " << dCos << std::endl;
return 0;
}

Passing Arrays to Functions by Reference

Sample Lab Task 7:

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

Pointers (Allocation and De-allocation)


Sample Lab Task 8:

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.

(Passing Arrays to Functions)


Task 5:

Implement the insertion sort algorithm. The function prototype is:

void insertion Sort(int list[ ], int size)

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;

const int MAXNAME = 10;

int main()
{
int pos;
char * name;
int * one;
int * two;
int * three;
int result;

// Fill in code to allocate the integer variable one here

// Fill in code to allocate the integer variable two here

// Fill in code to allocate the integer variable three here

// Fill in code to allocate the character array pointed to by name

cout<< "Enter your last name with exactly 10 characters." <<endl;


cout<< "If your name has <10 characters, repeat last letter."
<< "Blanks at the end do not count." <<endl;

for (pos = 0; pos < MAXNAME; pos++)

cin>> // Fill in code to read a character into the name array


//without using a bracketed subscript
cout<< "Hi ";

for (pos = 0; pos < MAXNAME; pos++)


cout<<// Fill in code to a print a character from the name array
// without using a bracketed subscript

cout<<endl<<"Enter three integer numbers separated by blanks" <<endl;

// Fill in code to input three numbers and store them in the


// dynamic variables pointed to by pointers one, two, and three.
// You are working only with pointer variables

//echo print
cout<< "The three numbers are " <<endl;

// Fill in code to output those numbers


result = // Fill in code to calculate the sum of the three numbers
cout<< "The sum of the three values is " << result <<endl;
// Fill in code to deallocate one, two, three and name
return 0;
}

You might also like