Skip to main content

Practice Problem #1 Solutions.

Tutorial on Practice Problem #1.


Problem 1 - It is simply a mixture of if statements.
Solution -

#include<iostream>
using namespace std;
#define and &&		// Defining it to make it easier to think of statements,
			// It will have no effect on runtime of a program,
			// As every "and" will be replaced by && at compile time itself
int main()
{
	int temperature;
	cout << "What is temperature today ? "; cin >> temperature;

	if(temperature > 20 and temperature < 25)
		cout << "The weather is Good" << endl;

	else if(temperature > 15 and temperature < 20)
		cout << "The weather is little cold" << endl;

	else if(temperature < 15)
		cout << "The weather is cold" << endl;

	else if(temperature > 25 and temperature < 30)
		cout << "The weather is little warm" << endl;
	
	else if(temperature > 30 and temperature < 40)
		cout << "The weather is warm" << endl;
	
	else if(temperature > 40)	// Figure out, why I am not writing at it as only "else"
		cout << "The weather is really hot" << endl;

	return 0;
}


Problem 2 - It is simple function implementation.

Solution - 

#include<iostream>
using namespace std;

void initialize(int *arr, int n)
{
	for(int i = 0; i< n; i++)
	{
		arr[i] = (i+1)*2;	// Not taking 0 as multiple, but it's just a choice.
	}
}
int main()
{
	int *arr;
	arr = new int[10];	// size is your choice.

	initialize(arr, 10);

	for(int i = 0; i< 10; ++i)
		cout << arr[i] << " ";
	cout << endl;
}


Problem 3 - This program is little lengthy and you have to use dynamic allocation, because you have to create 2-d array according to the input given by the user and to allocate memory at runtime we use pointer and heap memory.

Explanation of the algorithm - I created two function, one is for multiplying two matrices and storing it in the third matrix and one is for initializing these matrices before doing multiplication.

Also See carefully that while passing two dimensional array two my function I used pointer to pointer.

which means I created whole array in the heap and stored the address of first element in the stack memory. Here, de-allocation is not very important as after the multiplication the program is already at its end position of terminating, so the whole memory will be automatically freed. I declared the dimension (dim) at the top scope (Global scope), because it is needed in every function I used, so better declaring at the top and using it rather than passing it to every function as an argument.

Solution -

#include<iostream>
using namespace std;

// Considering the assumption I provided the function should be
int dim;
void mulitply(int **m1, int **m2, int **res)
{

	for(int i = 0; i< dim; i++)
	{
		for(int j = 0;j < dim; j++)
		{
			int temp = 0;
			for(int k = 0;k < dim; k++)
			{
				temp += (m1[i][k] * m2[k][j]);
			}
			res[i][j] = temp;
		}
	}
}
void initialize(int **matrix)
{
	for(int i = 0; i < dim; ++i)
	{
		for(int j = 0; j < dim; j++)
		{
			cout << "Enter " << i+1 << "," << j+1 << " element ";
			cin >> matrix[i][j];
		}
	}
}
int main()
{
	cout << "Enter the dimension of your square matrix : "; cin >> dim;

	int **matrix1 = new int*[dim];
	for(int i = 0; i< dim; i++)
		matrix1[i] = new int[dim];

	int **matrix2 = new int*[dim];
	for(int i = 0; i< dim; i++)
		matrix2[i] = new int[dim];

	int **matrix3 = new int*[dim];
	for(int i = 0; i< dim; i++)
		matrix3[i] = new int[dim];


	cout << "Initialize first matrix"<< endl;
	initialize(matrix1);
	cout << "Initialize second matrix"<< endl;
	initialize(matrix2);

	mulitply(matrix1, matrix2, matrix3);

	for(int i = 0 ;i< dim; i++)
	{
		for(int j = 0; j < dim; j++)
			cout << matrix3[i][j] << " ";
		cout << endl;
	}

}

You have to be very careful while using pointers, as while using these, the chances of mistakes are very high.


Problem 4 - It is very simple program you can even define macros for doing tasks like this.

Solution -

#include<iostream>
using namespace std;

int Area(int n)
{
	return n*n;
}

int Volume(int n)
{
	return (n*n*n);
}
int main()
{
	int a;	// We only need one dimension for square or cube.
	cout << "Enter the dimension of your cube/square : "; cin >> a;

	cout << "Area of square with side " << a << " is : " << Area(a);
	cout << "Volume of cube with side " << a << " is : " << Volume(a);
}


Problem 5 - In this problem also you have to use dynamic allocation, I will showing some ways, where we can try to reduce the use of dynamic allocation, but better thing is to practice as much as you can.

Solution - 

#include<iostream>
using namespace std;
int *func(int *arr, int n)
{
	int *temp = new int[n];		// n value is evaluated at runtim so dynamic allocation
	for(int i = 0; i< n;i++)
	{
		temp[i] = 2*arr[i];
	}
	return temp;
}
int main()
{
	int arr[] = {1,2,3,5,6,6,7,8,9};
	int n = sizeof(arr)/sizeof(arr[0]); // To find number of elements in an array(here arr)
	int *another = func(arr, n);

	for(int i = 0; i< n; i++) 	cout << another[i] << " ";
	cout << endl;

	return 0;
}


Bonus Problem

I have provided the solution already, Now, let's discuss what was wrong in our problem. Why the last number is printed as negative? 

The reason - We already have discussed about long long integers in data types sections and we very well know that long long integer has ranges from (-2^63-1 to 2^63), but when we found 91st and 92nd fibonacci numbers and add them we get some value which is even greater than 2^63(positive range), so, what happens is when the range reached 2^63 it again starts counting from (-2^63-1), which is the reason of getting negative values, and the thing we discussed just now is called overflow, means Overflowing the datatypes with some number greater than its range, underflow is just reverse, means giving so smaller value than number have in its negative range. We could have avoided it for 93rd value of fibonacci number by using unsigned long long int array but after that overflow would again occur.

Comments

Popular posts from this blog

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a...

Creating and executing c++ program.

In this slide we will discuss the following things. Creating an C++ file. Writing a Program and learning every aspect of its execution like. compilation, assembling and linking of library functions. Creating C++ File You should skip to " Your First Code ", if you know how to do this .😉 C++ program is nothing but a file with extension ".cpp". Creating a C++ file is not difficult. You can create it in anyway you like, I gave a simple way for creating. (Note - If you have not installed c++ compiler in your computer. Then, to run the c++ files, you must install  the compiler first or you can use some online c++ compiler like onlinegdb ) Your First Code Now, start typing the following code in your ".cpp" that you created. (This is also called as source code and you can consider it as the basic template for your c++ program, but I will progressively introduce more things that you can add to your c++ program). 1 2 3 4 5 6 #include<iostream> int main () ...

Number Systems (Binary and Decimal Number Systems).

We are surrounded with Numbers.  For example a human being has 2 ears, 2 legs(generally) and one nose. These all are nothing but numbers. Now, one might ask why do we need to study numbers for c++ programming. Let me answer this one first. In the previous blog I discussed that every computer works on machine language i.e. 0s and 1s, everyone know 0 and 1 are numbers. Ok, done, But what the word "Systems" doing here? What is meant by number systems. Let's see. We all have studied about the numbers from 0 to 9 i.e. 0,1,2,3,4,5,6,7,9 and we very well know that every other number can be derived from these number except infinity(no one knows what's that). for example one thousand twenty two is 1022.  This number system that we have studied is called Decimal Number System.  Why, Decimal? Because this number system contains 10 different symbols.  (In Greek Deca or Deka means 10 and this words is derived from Deca/Deka). Ok, Now we know what is decimal number system and why...