Recent Changes - Search:

Home Page

Student Resources

Connect with us:

Full Length Courses

Short Courses

Course Archive Information

Class Wikis

Socials

Various tips:

PmWiki

Edit SideBar

End Navigation

Chapter3

Chapter 3

3.1.1

Image 1:

This image shows a sign post with multiple pointers showing directions, these have been written out below.

Pointer, arr, computer, hard disc, pipe, float, arr[2], function, memory, computer.

3.1.2

Image 1:

This image shows the basic command for pointers which has been written out below.

int i;

3.1.3

Image 1:

This image shows the pointer expression which is written out below.

int *p;

3.1.4

Image 1:

This image shows a pointer assigned with a value, which has been written out below.

P = 148324;

3.1.5

Image 1:

This image is showing a pointer assigned with a zero value, this is written out below.

p = 0;

3.1.6

Image 1:

This image shows a pointer assigned with a null value, this has been written out below.

p = NULL;

3.1.7

Image 1:

This image shows a pointer with the reference operator, which is written out below.

p = &i;

3.1.8

Image 1:

This is an image of two blue coloured squares, one called p and the other i. The first square is in the centre of the screen and has a smaller yellow coloured rectangle on the top with the letter p positioned in the middle in bold black text. The second square is at the bottom right hand corner of the screen which also has a yellow coloured rectangle on the top with the letter i in the middle in bold black text, between the two squares a bold black arrow is pointing diagonally from the square with the letter p to the square with the letter i.

3.1.9

Image 1:

This is an image of two blue coloured squares, one called p and the other i. The first blue square has a smaller yellow coloured rectangle positioned on the top with the letter p in the middle in bold black text, in the centre of the square is a black circle with a black bold line pointing downwards with three smaller slim rectangle shapes in varying sizes underneath the line. The second square has a smaller yellow coloured rectangle positioned on the top with the letter i in the middle in bold black text. The first square represents p = null.

3.1.10

Image 1:

This image shows the dereferencing statement which is written out below.

int ivar, *ptr;

3.1.11

Image 1:

This image shows assigning a variable which has been written out below.

ivar = 2; 

3.1.12

Image 1:

This image shows the pointer point to a variable; this has been written out below.

ptr = &ivar;

3.1.13

Image 1:

This image shows the dereferencer operator, this has been written out below.

*ptr 

3.1.14

Image 1:

This image shows the dereferencer operator used in a cout statement, which has been written out below.

cout << *ptr;

3.1.15

Image 1:

This is an image of a value, pointed to by the pointer; this has been written out below.

*ptr = 4

3.1.16

Image 1:

This image shows an exclamation mark.

3.1.17

Image 1:

This image shows the sizeof operator, which is written out below.

sizeof

3.1.18

Image 1:

This image shows two examples of the sizeof operator; these have been written out below.

int i; char c;
i = sizeof c;

3.1.19

Image 1:

This image shows examples of the sizeof operator, which are written out below.

Char  tab[10];
i = sizeof tab;

3.1.20

Image 1:

This image shows examples of the sizeof operator, which have also been written out below.

char  tab[10];
i = sizeof tab[1];

3.1.21

Image 1:

This image is showing examples of the sizeof operator, which are written out below.

int i;
i = sizeof i;

3.1.22

Image 1:

This image shows a basic program with the sizeof operator, which has been written out below.

#include <iostream> 

using namespace std;

int main(void) { 
cout<< “This computing environment uses:” << endl;
cout<< sizeof(char) << “ bytes for chars”<< endl;
cout<< sizeof(short int) << “ bytes for chorts”<< endl;
cout<< sizeof(int) << “ bytes for ints”<< endl;
cout<< sizeof(long int) << “ bytes for longs”<< endl;
cout<< sizeof(float) << “ bytes for floats”<< endl;
cout<< sizeof(double) << “ bytes for doubles”<< endl;
cout<< sizeof(bool) << “ bytes for bool”<< endl;
cout<< sizeof(int *) << “ bytes for pointers”<< endl;
return 0;
}

3.2.1

Image 1:

This image shows a pointer with a three dimensional array in a single statement. This has been written out below.

int *ptr, Arr[3];

3.2.2

Image 1:

This image shows a diagram of what’s happening in the C++ statement which has been written out below.

int *Ptr, Arr[3];
Ptr = &Arr[0];
Ptr = Arr;

The image shows four blue coloured squares, the first square is positioned in the centre of the screen with a smaller yellow rectangle on the top with the letters ptr in the middle in bold black text. Squares two, three and four are positioned in a line next to each other in the right hand corner of the screen. On the top of each square is a smaller yellow rectangle with the letters Arr(0), Arr(1) and Arr(2) in bold black text, between the first square in the centre of the screen is a bold black arrow pointing diagonally to the three other squares.

3.2.3

Image 1:

This image shows the pointer mathematics operations which have been written out below.

int *ptr1, *ptr2, array[3], i;
ptr1 = array;

3.2.4

Image 1:

This image shows a diagram of what’s happening in the C++ statement which has been written out below with regard to pointers.

ptr2 = ptr1;

This image shows two red coloured squares positioned next to each other in the centre of the screen with the letters ptr1 and ptr2 in bold white text in the middle. Underneath the two top squares are three rectangles positioned next to each other, a red and two blue with the letters array(0), array(1) and array(2) in bold white text in the middle. There is a bold white arrow pointing downwards from the first and second square to the red rectangle below.

3.2.5

Image 1:

This is an image of pointer mathematics operations to see if two pointers are equal; this has been written out below.

if(ptr2 == ptr1){
:
:
}

3.2.6

Image 1:

This image shows a diagram of what’s happening in the C++ statement that is written out below with regards to pointer mathematics operations.

ptr2 = ptr2 + 1;
ptr2++;

This image shows two squares, the first square is red and the second is orange, they are positioned next to each other with a small space in-between. The letters ptr1 are positioned in the centre of the red square and the letters ptr2 in the centre of the orange square in bold white text. Underneath the red and orange squares are three rectangles, the first one is red the second one is orange and the third is blue, they are positioned next to each other in a straight line with array(0), array(1) and array(2) in bold white text in the centre of each rectangle. There is a bold grey coloured arrow pointing downwards from the red square to the red rectangle and another arrow pointing downwards from the orange square to the orange rectangle.

3.2.7

Image 1:

This image shows a pointer statement for a mathematical expression for subtraction, this has been written out below.

i = ptr2 – ptr1;

3.2.8

Image 1:

This image shows a pointer statement for a mathematical expression for addition, this is written out below.

ptr1 = ptr1 + 2;

3.2.9

Image 1:

This image shows a pointer diagram which has been written out below.

There are two squares next to each other, the first square is coloured orange and is positioned in the centre of the screen and the second square is blue and is positioned on the right side of the orange square. The letters ptr1 and ptr2 are in bold white text in the middle of the squares. Underneath the two squares are three rectangles, the first rectangle is red the second is orange which is positioned directly under the orange square and the third is blue which is positioned directly under the blue square with array(0), array(1) and array(2) in bold white text in the middle of each rectangle. There is a bold grey coloured arrow pointing downwards from the orange square to the orange rectangle and another arrow pointing downwards from the blue square to the blue rectangle.

3.2.10

Image 1:

This is an image of a pointer statement for a mathematical expression used for subtraction, this has been written out below.

ptr2 = ptr2 – 1;

3.2.11

Image 1:

This image shows a pointer diagram which is written out below.

There are two coloured squares next to each other, a red one and a blue one, the red square is positioned in the centre of the screen and the blue square is to the right of the screen with an empty space in the middle. The letters ptr2 are in bold white text in the centre of the red square and the letters ptr1 also in bold white text are in the centre of the blue square. Underneath the squares are three rectangles. Rectangle one is coloured red and is positioned directly under the red square, rectangle two is orange and next to the red rectangle and the third rectangle which is positioned next to the orange is blue which is directly under the blue square. In the centre of each rectangle in bold white text are tab(0), tab(1) and tab(2). There is a bold grey coloured arrow pointing downwards from the red square to the red triangle and another grey coloured arrow pointing downwards from the blue square to the blue rectangle.

3.2.12

Image 1:

This is an image of a pointer statement used for a mathematical expression of subtraction which has been written out below.

ptr1 – ptr2

3.3.1

Image 1:

This is an image of a sign post with the word function written in bold orange text.

3.3.2

Image 1:

This image is shows a computer monitor with a C++ program shown on the screen.

3.3.3

Image 1:

This image shows an example of a function declaration; this has been written out below.

float square(float x);

3.3.4

Image 1:

This image shows an example of a function declaration which is written out below.

float square(float x)
{
	float result;
	result = x * x;
	return result;
}

3.3.5

Image 1:

This image shows a program using function declaration. The code has been written out below.

#include <iostream>
using namespace std;
float square(float x);
{
	float result;
	result = x * x;
	return result;
}
int main(void){
	float arg = 2.0;
	cout << ”The second power of ”<<arg<<” is “<<square(arg)<<endl;
	return 0;
}

3.3.6

Image 1:

This is an image of a program using function declaration which has been rearranged and is written out below.

#include <iostream>
using namespace std;
float square(float x);
int main(void){
	float arg = 2.0;
	cout << ”The second power of ”<<arg<<” is “<<square(arg)<<endl;
	return 0;
{

float square(float x);
{
	float result;
	result = x * x;
	return result;
}

3.4.1

Image 1:

This image shows an example of a function declaration which is written out below.

return_type function_name( parameters_list);

3.4.2

Image 1:

This image shows an example of a function definition which has been written out below.

return_type function_name( parameters_list);
{
	Function_body;
}

3.4.3

Image 1:

This is an image of a function definition which is written out below.

void Greet(void);
{
	cout<<” Ave user! “<<endl;
}

3.4.4

Image 1:

This image shows a function definition with camelcase; this is written out below.

void GreetManyTimes(int howmanytimes)
{ 
	while(howmanytimes > 0)
	{
		Greet();
		Howmanytimes--;
	}
}

3.4.5

Image 1:

This image shows a function definition for a complete program which shows functioning of the functions. The code has been written out below.

#include <iostream>
using namespace std;
void Greet(void)
{
	cout<<”Ave user!”<<endl;
}

void GreetManyTimes(int howmanytimes)
{
	while(howmanytimes > 0)
 	{
		Greet();
		howmanytimes--;
	}
}

int main(void)
{
	int sizeofego;
  	cout<<”How big is your ego? [km]”<<endl;
	cin>>sizeofego;
	GreetManyTimes(1 + sizeofego);
	Return 0;
}

3.4.6

Image 1:

This image shows a function definition for a complete program which converts temperature in Fahrenheit to Celsius with the test the function; this has been written out below.

#include <iostream>
using namespace std;

float FahrenheitToCelsius(float temp)
{
	return((temp – 32.0) * 5.0) / 9.0;
}

void TestTheFunction(float temp) {
	cout<<”Fahrenheit”<<temp<<”correspondsto”<<
FahrenheitToCelsius(temp)<<” Centigrade”<<endl; 
}
int main(void)
{
	TestTheFunction(32.0);
	TestTheFunction(212.0);
	TestTheFunction(451.0);
	return 0;
}

3.4.7

Image 1:

This image shows two new functions, void and non-void, these have been written out below.

void VoidFunction(int par) {…; return;}
int NonVoidFunction(int Par) {…; return par * par;}

3.5.1

Image 1:

This image shows an example of a global variable and side effects in the complete program; this example has been written out below.

#include<iostream>
using namespace std;
int globvar = 0;

void func(void)
{
	cout<<”Thank you for invoking me :)”<<endl;
	globvar++
}
int main (void)
{
	for(int i = 0; i < 5; i++)
		func();
	cout<<endl<<”The function enjoyed”<<globvar<<”times”<<endl;
	return 0;
}

3.5.2

Image 1:

This image shows a new function called AmIAbleToChangeMyParameter in the program; this has been written out below.

#include <iostream>
using namespace std;
void AmIAbleToChangeMyParameter(int param)
{
	cout<<”------------“<<endl;
	cout<<”I have got: “<<param<<endl;
	param++;
	cout<<”I’m about to give back: “<<param<<endl;
	cout<<”------------“<<endl;
}
int main (void)
{
	int var = 1
	cout<<”var = “<<var<<endl;
	AmIAbleToChangeMyParameter(var);
	cout<<”var = “<<var<<endl;
	return 0;
} 

3.5.3

Image 1:

This image shows a program using an example of passing by reference, which affects the actual parameter values in the program; this has been written out below.

#include <iostream>
using namespace std;
void AmIAbleToChangeMyParameter(int &param)
{
	cout<<”------------“<<endl;
	cout<<”I have got: “<<param<<endl;
	param++;
	cout<<”I’m about to give back: “<<param<<endl;
	cout<<”------------“<<endl;
}
int main (void)
{
	int var = 1
	cout<<”var = “<<var<<endl;
	AmIAbleToChangeMyParameter(var);
	cout<<”var = “<<var<<endl;
	return 0;
} 

3.5.4

Image 1:

This image shows a new function called: MixedStyles which uses passing by value and passing by reference in the one program, this has been written out below.

#include <iostream>
using namespace std;
void MixedStyles(int bval, int &bref)
{
	Bref = bval + 1;
}
int main(void)
{
	int var1 = 1, var2;
	MixedStyles(var1, var2);
	cout<<”var1 = “<<var1<<”,var2 = “<<var2<<endl;
	return 0;
} 

3.5.5

Image 1:

This image shows an example of passing by reference and its limitation. The example has been written out below.

void ByRef(int &par)
{
	par = 0;
}

Void ByVal(int par)
{
	par = 0;
} 

3.5.6

Image 1: This image shows a new function called ByPtr which allows the user to alter values outside of the function and passing by value inside the function; this has been written out below.

#include <iostream>
using namespace std;
void ByPtr(int *ptr)
{
	*ptr = *ptr + 1;
}

int main(void)
{
	int variable = 1;
	int *pointer = &variable;
	ByPtr(pointer);
	cout<<”variable = “<<variable<<endl;
	return 0;
}

3.6.1

Image 1:

This image shows a new function called NewGreet, which has been written out below.

#include <iostream>
using namespace std;
void NewGreet(string greet, int repeats)
{
	for(int i = 0; i < repeats; i++)
		cout<<greet<<endl;
}
int main(void)
{
	NewGreet(“Hi!”,5);
	Return 0;
}

3.6.2

Image 1:

This image shows the use of default parameters for the function called NewGreet; these have been written out below.

#include <iostream>
using namespace std;
void NewGreet(string greet, int repeats = 1)
{
	for(int i = 0; i < repeats; i++)
		cout<<greet<<endl;
}
int main(void)
{
	NewGreet(“Hello”,2);
	NewGreet(“Good Morning”);
	NewGreet(“Hi”,1);
	Return 0;
}

3.6.3

Image 1:

This image shows the use of multiple default parameters for the function called NewGreet; these have been written out below.

#include <iostream>
using namespace std;
void NewGreet(string greet = “Good morning”, int repeats = 1)
{
	for(int i = 0; i < repeats; i++)
		cout<<greet<<endl;
}
int main(void)
{
	NewGreet(“Hello”,2);
	NewGreet(“Hi”);
	NewGreet();
	Return 0;
}

3.7.1

Image 1:

This image shows an example of function invocation; this has been written out below.

#include <iostream>
using namespace std;
int function(int parameter)
{
	return parameter *2;
}

int main(void)
{
	int var = 1;
	var = function(var);
	var = function(var);
	var = function(var);
	cout<<var<<endl;
	return 0;
}

3.7.2

Image 1:

This image shows a diagram of the function invocation which is written out below.

There are five rectangles which have been positioned to form the letter V. From the top left the first rectangle is purple in colour and has the word main in bold white text, below the rectangle is an arrow pointing to the second rectangle which is lilac in colour with the word prologue, underneath is another arrow pointing to the third rectangle which is blue in colour with the word function in bold white text. At the right side of the rectangle is an arrow pointing upwards to the fourth rectangle which is a lighter blue in colour with the word epilogue, at the right side of this rectangle is an arrow pointing upwards to the last rectangle which is also a lighter blue in colour with the word main in bold white text.

3.7.3

Image 1:

This image shows a diagram of function inlining which has been written out below.

There are three large rectangles directly underneath each other in a column. The first rectangle is purple in colour with the word main written in white text, the second rectangle is blue with the word function and the third rectangle is light blue with the word main.

3.7.4

Image 1:

This image shows an example of an inline function which has been written out below.

#include <iostream>
using namespace std;
inline int function(int parameter)
{
	return parameter *2;
}

int main(void)
{
	int var = 1;
	var = function(var);
	var = function(var);
	var = function(var);
	cout<<var<<endl;
	return 0;
}

3.8.1

Image 1:

This image shows an example of functions with the same name but perform different purposes, these are written out below.

float max(float a, float b)
{
	if(a > b)
		return a;
	else
		return b;
}

3.8.2

Image 1:

This image shows an example of functions with the same name but perform different purposes using a function called Max to find the biggest number of three variables, these have also been written out below.

float max(float a, float b, float c)
{
	int m = a;
	if(b > m)
	     m = b;
	if(c > m)
	     m = c;
	return m;
}

3.8.3

Image 1:

This image shows an example of overloading for functions, this is written out below.

float max(float a, float b, float c)
{
	int m = a;
	if(b > m)
	     m = b;
	if(c > m)
	     m = c;
	return m;
}

3.8.4

Image 1:

This image is showing an example of two overloading functions; these have been written out below.

void PlayWithNumber(int x) {……}
void PlayWithNumber(float x) {…..}
:
PlayWithNumber(1);
:

3.8.5

Image 1:

This image shows an example of find best candidate, which is written out below.

void PlayWithNumber(int x) {……}
void PlayWithNumber(float x) {…..}
:
PlayWithNumber(1.0);
:

3.8.6

Image 1:

This image shows a new operator which has the question mark first and the colon punctuation mark second; this has been written out below.

expression1 ? expression2 : expression3

3.8.7

Image 1:

This image shows an example of the new operator which has the question mark first and the colon punctuation mark second, this is also known as the ternary operator and has been written out below.

float max(float a, float b)
{
	Return a > b ? a : b;
}

3.8.8

Image 1:

This image shows a picture of another example of the ternary operator written out below.

float max(float a, float b, float c)
{
	Return a > b ? (a > c ? a : c) : (b > c ? b : c);
}

3.9.1

Image 1:

This image shows a diagram of five large yellow boxes next to each other in a straight line across the screen with numbers ranging from ten to two. The boxes are read from left to right and the numbers have been written out below.

The first box is number 8, the second is number 10, the third is number 6, the fourth is number 2 and the fifth box is number 4.

3.9.2

Image 1:

This image shows an array sorting diagram. There are five large yellow boxes in a straight line across the screen with numbers ranging from ten to two. The boxes are read from left to right and the numbers have been written out below.

The first box is number 8, the second is number 6, the third is number 10, the fourth is number 2 and the fifth box is number 4. The numbers 6 and 10 are in red text while all the other numbers are in black text. This means that in an unsorted array the numbers in the boxes will be 8,10,6, 2 and 4 while in a sorted array the numbers will be 8,6,10,2 and 4, which is why the numbers 6 and 10 are in red text because they have been swapped around.

3.9.3

Image 1:

This image shows another array sorting diagram, below are examples of an unsorted and sorted array.

Unsorted array: The first box is number 8, the second is number 6, the third is number 10, the fourth is number 2 and the fifth box is number 4.

Sorted array: The first box is number 8, the second is number 6, the third is number 2, the fourth is number 10 and the fifth box is number 4.

3.9.4

Image 1:

This image is of another array sorting diagram. The examples of an unsorted and sorted array are written below

Unsorted array: The first box is number 8, the second is number 6, the third is number 2, the fourth is number 10 and the fifth box is number 4.

Sorted array: The first box is number 8, the second is number 6, the third is number 2, the fourth is number 4 and the fifth box is number 10.

3.9.5

Image 1:

This image shows a diagram of an array known as a bubble sort. There are five large yellow boxes directly underneath each other in a column, the numbers are read from the top to the bottom and the numbers range from ten to two. These have been written out below.

The first number is 10, the second is 4, the third is 2, the fourth is 6 and the fifth number is 8.

3.9.6

Image 1:

This image shows an array sorting diagram where the five yellow boxes are in a horizontal line instead of a column, again with the numbers ranging from two to ten. These have been written out below.

Unsorted array: The first box is number 8, the second is number 6, the third is number 2, the fourth is number 4 and the fifth box is number 10.

3.9.7

Image 1:

Here is another array sorting diagram. The examples of an unsorted and sorted array have been written below.

Unsorted array: The first box is number 8, the second is number 6, the third is number 2, the fourth is number 4 and the fifth box is number 10.

Sorted array: The first box is number 6, the second is number 8, the third is number 2, the fourth is number 4 and the fifth box is number 10.

3.9.8

Image 1: this is another example of an array sorting diagram.

Unsorted array: The first box is number 6, the second is number 8, the third is number 2, the fourth is number 4 and the fifth box is number 10.

Sorted array: The first box is number 6, the second is number 2, the third is number 8, the fourth is number 4 and the fifth box is number 10.

3.9.9

Image 1:

This image shows another example of an array sorting diagram. These examples have been written out below.

Unsorted array: The first box is number 6, the second is number 2, the third is number 8, the fourth is number 4 and the fifth box is number 10.

Sorted array: The first box is number 6, the second is number 2, the third is number 4, the fourth is number 8 and the fifth box is number 10.

3.9.10

Image 1:

This image shows another example of an array sorting diagram. The examples are written out below.

Unsorted array: The first box is number 6, the second is number 2, the third is number 4, the fourth is number 8 and the fifth box is number 10.

Sorted array: The first box is number 2, the second is number 6, the third is number 4, the fourth is number 8 and the fifth box is number 10.

3.9.11

Image 1:

Here is another example of an array sorting diagram.

Unsorted array: The first box is number 2, the second box is number 6, the third is number 4, the fourth is number 8 and the fifth box is number 10.

Sorted array: The first box is number 2, the second box is number 4, the third is number 6, the fourth is number 8 and the fifth box is number 10.

3.9.12

Image 1:

This image shows an example of sorting an array in C++, also known as bubble sort; this has been written out below.

int numbers[5]; //array to be sorted
int aux;		//auxiliary variable for swaps

// we need 5 – 1 comparisons – why?
for(int i = 0; i < 4; i++){
	//compare adjacent elements
	if(numbers[i] > numbers[i + 1]){
	/* if we went here it means that we have to swap the elements */
	aux = numbers[i];
	numbers[i] = numbers[i + 1];
	numbers[i + 1] = aux;
	}

3.9.13

Image 1:

This image shows another example of sorting an array in C++, also known as bubble sort; this has also been written out below.

int numbers[5]; 
int aux;	
bool swapped;

do{  //we will decide if we need to continue this loop
    swapped = false;  //no swap occurred yet

    for(int i = 0; i < 4; i++)
         if(numbers[i] > numbers[i + 1]){
	swapped = true;
	aux = numbers[i];
	numbers[i] = numbers[i + 1];
	numbers[i + 1] = aux;
	}
    }while(swapped);

3.9.14

Image 1:

This image shows another example of sorting an array in C++, also known as bubble sort-final version; this is written out below.

#include <iostream>
using namespace std;

int main(void){
int numbers[5]; 
int aux;	
bool swapped;
// ask the user to enter 5 values
for(int i = 0; i < 5; i++){
	cout<<endl<<”Enter Value #”<<i + 1<<”:”;
	cin>>numbers[i];
}
// sort them
do{
	swapped = false;
	for(int i = 0; i < 4; i++){
	  if(numbers[i] > numbers[i + 1]){
		swapped = true;
			aux = numbers[i];
			numbers[i] = numbers[i + 1];
		numbers[i + 1] = aux; 
			}
		}
	}while(swapped);
	// print results
	cout<<endl<<”Sorted array: “<< endl;
	for(int i = 0; i < 5; i++)
		cout << numbers[i]<<” “;
	cout<<endl;
	return 0;
}

3.10.1

Image 1:

This image shows an example of the void type which has been written out below.

void nothingatall(void);

3.10.2

Image 1:

This image is showing an example of the void type with a pointer. This has been written out below.

void *ptr;

3.10.3

Image 1:

This image shows two new keywords to manage and create memory blocks in C++. These have been written out below.

new, delete

3.10.4

Image 1:

This image shows two examples on how to create new memory blocks in C++; these have been written out below.

float *array = new float[20]
int  count = new int;

3.10.5

Image 1:

This is an image of two examples on how to delete a memory block in C++; these have been written out below.

delete [] array;
delete count;

3.10.6 Image 1:

This image shows how to create and delete memory blocks in a complete C++ program which has been written out below.

#include<iostream>
using namespace std;

int main(void){
    float *arr;

arr = new float[5];
for(int i = 0; i < 5; i++)
    arr[i] = i * i;
for(int i = 0; i < 5; i++)
    cout<<arr[i]<<endl;
delete [] arr;
return 0;
}

3.10.7

Image 1:

This image shows the updated bubble sort algorithm program which is written out below.

int *tabptr, sum = 0;

tabptr = new int[5];
for(int i = 0; i < 5; i++)
   tabptr[i] = i;
sum = 0;
for(int i = 0; i < 5; i++)
      sum += tabptr[i];
delete [] tabptr;

3.10.8

Image 1:

This image shows an even better and improved bubble sort algorithm program which has been written out below.

#include<iostream>
using namespace std;
int main(void){
	int *numbers, how_many_numbers;
	int aux;
	bool swapped;

cout<<” How many numbers are you going to sort? “;
cin>> how_many_numbers;
if(how_many_numbers <= 0 || how_many_numbers > 1000000){
	cout<<” Are you kidding?”<<endl;
	return 1;
}
numbers = new int[how_many_numbers];
for(int i = 0; i < how_many_numbers; i++){
	cout<<”\n Enter the number #”<< i + 1<< “:”;
	cin>> numbers[i];
}
do{
swapped = false;
for(int i = 0; i < how_many_numbers – 1; i++)
    if(numbers[i] > numbers[i + 1]){
	swapped = true;
	aux = numbers[i];
	numbers[i] = numbers[i + 1];
	numbers[i + 1] = aux;
      }
}while(swapped);
cout<<endl<<”The sorted array:”<<endl;
for(int i = 0; i < how_many_numbers; i++)
     cout<<numbers[i]<<” “;
cout<< endl;
delete [] numbers;
return 0;
}
Edit - History - Print - Recent Changes - Search
Page last modified on August 01, 2017, at 02:07 AM