מבוא למדעי המחשב תרגיל 4

benny++C, Loops, מבוא למדעי המחשבLeave a Comment

פתרון שאלה 1

/*************************
targil bait 4 shela 1 
discription: display 10 random numbers and Check if the numbers list are sorted list  or not.
**************************/



#include <iostream>
#include <ctime>
using namespace std;

int main()
{

	
	srand(time(NULL));     //   to connect the program to the clock for random numbers

	
	int randSum = 0, sortedSum = 0 ,randNum;

	for (int i = 0; i < 10; i++)           
	{
			
			randNum = rand () % 1000 + 1;       // to make shur that the number is between 1 to 1000
			 
			cout << randNum << " ";
		
			randSum +=randNum;                  //  to recive the sum of all the random numbers no metter what the order is .

			while (randNum>=sortedSum)           // to make shur that: every random number is Larger than the previous one ("sorted list")
			{
				sortedSum +=randNum;
				
			}

	}

		if (randSum==sortedSum)              // its meen:  every random number is Larger than the previous one ,Because only in such a case like that: randSum = randSum
		{									 
			cout<<endl;
			cout <<"sorted list"<<endl;
		}
		else
		{
			cout<<endl;
			cout <<"not sorted list"<<endl;
			
		}
	



//	system("pause");
	return 0;

}
/************OUTPUT*************
690 797 268 12 567 405 249 512 806 19
not sorted list
**************************/
	

	

פתרון שאלה 2

/*************************
targil: targil 4 sheela 2 
discription: loop with 2 Conditions
**************************/


#include <iostream>
using namespace std;

int main ()
{
	int positNum1, positMun2;

		cout<< "enter 2 positive numbers:"<<endl;
	do
	{
		cin>> positNum1;
		if (positNum1<=0)
		{
			cout<<"ERROR"<<endl;
		}
	} while (positNum1<=0);     // if the user enter incorrect input for number 1, the program give him chance to input another number


		do
	{
		cin>> positMun2;
		if (positMun2<=0)
		{
			cout<<"ERROR"<<endl;
		}
	} while (positMun2<=0);     // if the user enter incorrect input for number 1, the program give him chance to input another number


	cout<< "enter a list of numbers:"<<endl;

	int list, listSum=0, quantityNum=0;

	
	for (listSum = 0; listSum <= positNum1&&quantityNum<positMun2; listSum+=list)    // loop with 2 Conditions: 1.if the list sum biger from the first positive number, stop!. 2.  if the quantity of the list nubers=second positive number, stop!.
	{	 	  	 	   	   		   	 	    	 	
		cin>>list;
		quantityNum++;
		
	} 

	cout<<listSum<<endl;
	
	//system ("pause");
	return 0;


}


	
	/*********input*****8
	                                  
	enter 2 positive numbers:
	25
	12
	enter a list of numbers:
	9 8 7 6
	30

	*********************/	 	  	 	   	   		   	 	    	 	

פתרון שאלה 3

/*************************
targil: 4 sheela 3
discription: fibonaci sirias. the user enter the 'n', the program show the fibonaci sirias forom the Beginning to this 'n'.
**************************/

#include <iostream>
using namespace std;

int main()
{
	int n, first, second;
	cout << "enter a number:" << endl;
	
	do
	{
		cin >> n;
		if (n<0)
		{
			cout << "ERROR" << endl;
		}
	} while (n<0);                  // if the user enter incorrect input, the program give him chance to input another number

	int num = n;            //   to save the n value to know how many time to ran the loop (n value change in the loop so i craete another Variable)

	first = 1;            
	second = 0;

	if (n==0)              
	{
		cout<<"0"<<endl;    // if the input is '0' we dont need loop for that case.
	}	

	else
	{
		cout<<"0 1" <<" ";        //   if the input is  mor then '0' the screen always have to shoe "0 1" and later go into the loop

			for (int i = 0; i < num-1; i++)
		{	 	  	 	   	   		   	 	    	 	
			n = first + second;

			first = second;

			second = n;

			n = first + second;
				
			cout << n << " ";
		

		}


	}

		cout<<endl;

//	system("pause");
	return 0;
}


	/*********OUTPUT*****8
	                                  

	enter a number:
	7
	0 1 1 2 3 5 8 13


	*********************/	 	  	 	   	   		   	 	    	 	

פתרון שאלה 4

/*
Targil: 4 sheela 4
LECTURER: Meir Komer
DESCRIPTION: the user enter 'n', the progrm provide the sum of this sirias " X + (-1/3)X3  + (1/5)X5 + (-1/7)X7 + …. +((-1)^ (n-1)/2n-1)*X^(2n-1) "  from n=1 to the input 'n', 
***************************************/
#include <iostream>
using namespace std;

int main()
{
	int x, n, hold_n, hold_x, xExp, one = 1;
	float mekadem, sum = 0, an;

	cout << "enter 2 numbers;" << endl;
	cin >> x >> n;
	while (n <= 0)              // for incorrect input (only for 'n', x can be any number.
	{
		cout << "ERROR" << endl;
		cin >> n;
	}


	hold_x = x;     // to keep the n value to know how many time the program will run the loop.
	hold_n = n;
	xExp = x;

	for (int j = 0; j < hold_n; j++)
	{
		for (int i = 1; i <(2 * n - 1); i++)       // (2*n -1) is the exponent, the loop shulde run acording to the exponent number.
		{
			xExp *= x;

		}

		if (n % 2 == 0)         // if n is: 2 or 4 or 6 ..8..10 , the value of An shulde be negative, so the prograo Multiply (*-1)
		{	 	  	 	   	   		   	 	    	 	
			one *= -1;
		}
		mekadem = (float)one / (2 * n - 1);

		an = mekadem*(float)xExp;      // this is the value of An
		sum += an;                    // this is the sum of the sirias from n=1 to the unput n.
		n--;
		if (n % 2 != 0)       //  if n is: 3 or 5 or 7 ..9..11 , the value of An shulde be positive, so the prograo Multiply (*-1)
		{
			one *= -1;
		}


		xExp = x;   // return the initial value fo xExp for the next loop.
	}

	cout << sum << endl;











	//system("pause");
	return 0;
}
/************OUTPUT*************
enter 2 numbers;
3
2
-6
********************************
enter 2 numbers;
3
0
ERROR
-1
ERROR
-3
ERROR
4
-269.829
*******************************/

	 	  	 	   	   		   	 	    	 	

פתרון נוסף לשאלה 4

/*
LECTURER: MEIR KOMAR
TARGIL4
DESCRIPTION: PERFORMING A GIVEN SERIES OF CALCULATIONS, AND DISPALAYS THE FINAL SUM OF RESULTS.
***************************************/

#include<iostream>
using namespace std;
int main()
{
	int n;
	float x, y, result = 0, producer, minus1 = -1;
	
	cout << "enter 2 numbers:" << endl;
	cin >> x >> n;
	
	while (n < 1) //TO VALIDATE THE INPUT.
	{
		cout << "ERROR" << endl;
		cin >> n;
	}
	
	while (n > 0) //BECAUSE WE WILL MAYBE NEED THE SERIES TO RUN MORE THAN ONCE.
	{
		producer = x; //CALCULATIONS OF VARIABLE X.
		for (int i = 1; i < ((2 * n) - 1); i++) 
		{
			producer *= x;
		}

		minus1 = -1;
		for (int i = 0; i < n; i++) //CALCULATIONS OF THE REST.
		{
			minus1 *= (-1);
		}

		y = minus1 / ((2 * n) - 1);
	
		result += (y * producer); //TO COLLECT AND COMBINE THE RESULTS.
	
		n--; //TO DO THE NEXT PART OF THE SERIES.
	}

	cout << result << endl;
	

	system("pause");
	return 0;
}

/*************************************************
OUTCOMES:
1)
enter 2 numbers:
3
2
-6
Press any key to continue . . .
2)
enter 2 numbers:
9
-5
ERROR
0
ERROR
5
4.3718e+07
Press any key to continue . . .
*/

פתרון שאלה 5

#include <iostream>
using namespace std;




int main()
{

	cout<<"enter a one digit number:\n";
	int num;
	int space=0;
	cin>>num;

	for (int  j  = num;j>0 ; j--)
	{
		for (int k=0;  k< space; k++)
		{
			cout<<"  ";
		}
		for (int i = j; i > 0; i--)
		{
			cout<<i<<" ";
		} 
		cout<<endl;
		space++;
		
	}
	

	system("pause");
	return 0;
}


פתרון שאלה 6

/*************************
targil bait 4 shela 6
discription:  cheking a bihavyer of a nember
**************************/

#include<iostream>
using namespace std;
int main()
{
	int num, test, test2, counter1 = 0, counter2 = 0;

	cout << "enter a number:" << endl;
	cin >> num;

	while (num < 1) 
	{
		cout << "ERROR" << endl;       //for a negavie nember or  num=0
		cin >> num;
	}

	while (num > 0)
	{

		test = num % 10;       // give as the right number
		num /= 10;             //give as the  number  without the right number

		if (num == 0)
		{
			counter1++;
		}
		test2 = num % 10;     //  like the test bifore withe the next tow numbers
		num /= 10;

		counter2++;   

		if ((test2-test)==1)   // to seee if the diffrent between the numbers is 1
		{	 	  	 	   	   		   	 	    	 	
			counter1++;   
		}
	}

	if (counter1 == counter2)  
	{
		cout << "YES" << endl;
	}
	else
	{
		cout << "NO" << endl;
	}




	return 0;
}


/*********OUTPUT*****8***********************
					  *		
enter a number:       *   enter a number:
12345				  *	  54321
NO					  *   YES
*********************/	 	  	 	   	   		   	 	    	 	

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *