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

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

מערכים חד מימדיים ב ++C
arrays in ++C

פתרון שאלה 1

/*************************
targil: 6 sheela 1
lecturer: meir komer
discription: the user enter array with 15 numbers and the program chec if the numbers are sorted
**************************/
#include <iostream>
using namespace std;

int main()
{
	int a[15];
	int sum = 0;
	cout << "enter 15 numbers:" << endl;
	for (int i = 0; i < 15; i++)
	{
		cin >> a[i];
	}

	for (int i = 1; i < 16; i++)
	{
		
		for (int j = 0; j < 15; j++)
		{
			if (a[j] == i)
			{
				sum += a[j];
				break;
			}

		}
	}
	if (sum==120)    // its meam that all the numbers between 1-15.
	{
		cout << "good" << endl;
	}	 	  	 	   	   		   	 	    	 	
	else
	{
		cout << "not good" << endl;

	}




//system("pause");
	return 0;
}
/**********OUTPUT******************
enter 15 numbers:
1 2 3 54 5 6 4 5 6 3 2 1 08 5 6
not good

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

פתרון שאלה2

/*************************
targil: 6 sheela 2
lecturer: meir komer
discription: enter tow arrays (vector_1 and vector_2) the program can chack how many times vector_1 containing vector_2.
**************************/


#include <iostream>
using namespace std;



int main()
{
	int vector1[500], vector2[100];
	int vec1Num, vec2Num, count = 0, mach = 0, k = 0;

	cout << "enter size of first array:" << endl;
	cin >> vec1Num;
	cout << "enter first array values:" << endl;

	for (int i = 0; i < vec1Num; i++)  // input loop fot the first array
	{
		cin >> vector1[i];

	}

	cout << "enter size of second array:" << endl;

	cin >> vec2Num;
	cout << "enter second array values:" << endl;    

	for (int i = 0; i < vec2Num; i++)           // input loop fot the second array
	{
		cin >> vector2[i];

	}	 	  	 	   	   		   	 	    	 	

	for (int i = 0; i < vec1Num; i++)
	{
		count = 0;                          // to star counting from 0 again
		int w = 0;
			for (int j = 0; j < vec2Num; j++)
			{
				
				
				if (vector2[w] == vector1[j+i])
				{
					count += 1;
					w++;
				}
				else
				{
					w++;
				}

				if (count == vec2Num)
				{
					mach += 1;            // its mean : 1 mach found
				}
				
			}
			
	}

	cout << "result: " << mach << "  times" << endl;
//	system("pause");
	return 0;

}
/******OUTPUT**********
enter size of first array:
10
enter first array values:
1 2 3 1 2 3 1 2 3 0
enter size of second array:
3
enter second array values:
1 2 3
result: 3  times

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

פתרון שאלה3

/*************************
targil: 6 sheela 3
lecturer: meir komer
discription: the program can chack if arrays containing Duplicate numbers and Eliminating the doubles and display the rest numbers.
**************************/

#include <iostream>
using namespace std;




void chackArray(int chack[100],int i)

{
	
	for (int j = 0; j < i; j++)                // i is the number of elements of the input vector
	{
		for (int k = 0; k < i; k++)
		{

			if (chack[j]==chack[k]&&j!=k&&j<k)       // 3 tarms: 'tarm 1' if  chack[0]=chack[0] ...  this is always true so 'tarm 2' we need also to make shur that j!=k.     ....
			{
				chack[k]=0;                         // .... and 'tarm 3' j<k is to make shor to keep the first number value and turn the second number to be 0.
			}
		}
	}


	int count=0;                        // the value of count in the and of the loop is the number of elements of the new vector
	for (int w = 0; w < i; w++)
	{
		if (chack[w] != 0 )
		{
			cout <<chack[w] <<" ";
			count+=1;

		}	 	  	 	   	   		   	 	    	 	
	}
	cout <<endl;
	cout <<"number of elements:"<<count<<endl;

}







int main()

{
	int array[100];
	int i=0;     
	cout <<"enter up to 100  values, to stop enter 0:"<<endl;
	for ( i = 0; i < 100; i++)
	{
		cin >> array[i];
		if (array[i]==0)
		{
			break;
		}

		
	}

		cout << "the new vector is: "<<endl;

	chackArray(array,i);

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

פתרון שאלה4

/*************************
targil: 6 sheela 4
lecturer: meir komer
discription: the program can sorted 6 numbers acording to the values from the smaller to the bigger.
**************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
	float numbers [6];                                  // the first array (input by user)
	int i, small=0, size=6, place_1,place_2,place_3,place_4,place_5,place_6;

	cout <<"enter  6 numbers between 0 and 1:"<<endl;



	for ( i = 0; i < 6; i++)
	{
		cin>>numbers[i];
		if (numbers[i]<=0 || numbers[i]>=1)   // to make sure: if the input iset is not between (0,1) the user have to put in the numbers from the beginning.
		{
			cout<<"ERROR"<<endl;
			i=-1;
		}
	}



	for (int k = 0; k < 6; k++)
	{
			for (int i = 0; i < 6; i++)
			{
					for (int j = 0; j < 6; j++)
					{	 	  	 	   	   		   	 	    	 	
						
						if(numbers[i]<numbers[j])  //if the number smaller from the next one
						{
							small+=1;                   // so count one, if the number is the smaller one so "small" value will be 5 becauze it is mean that the condition is true for all the numbers
						}
						
					}
					if (small==size-1)               // in the next rotation size value is -1 
						{
							if (size==6)               //  if the condition is true 5 times:  (when size=6.the condition is 6-1 times
							{
								place_1=i;             // put this number un the first place
							}
							if (size==5)               // if the condition is true only 4 times its mean: the number us smaller only from 4 numbers
							{
								place_2=i;                  // put this number un the second place
							}
							if (size==4)               // if the condition is true only 3 times its mean: the number us smaller only from 3 numbers
							{
								place_3=i;                 // put this number in place_3
							}
							if (size==3)                // if the condition is true only 2 times its mean: the number us smaller only from 2 numbers
							{
								place_4=i;               // put this number in place_4
							}
							if (size==2)
							{
								place_5=i;
							}
							if (size==1)
							{
								place_6=i;
							}
							size-=1;
						}
	
				small=0;
			}	 	  	 	   	   		   	 	    	 	
	}



	int indices [6]={place_1,place_2,place_3,place_4,place_5,place_6};    // the new numbers array
	cout <<"sorted indices:"<<endl;
	for (int i = 0; i < 6; i++)
	{
		cout<<indices[i]<<" ";   
	}
	cout <<endl;

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

enter  6 numbers between 0 and 1:
0.3  0.56  0.4  0.215  0.0001   0.9
sorted indices:
4 3 0 2 1 5

*************************************************
enter  6 numbers between 0 and 1:
0.2  0.3  0.6  -2  0.2  0.5  0.4  0.7  0.9  0.25
ERROR
sorted indices:
0 5 2 1 3 4

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

פתרון שאלה5

/*************************
targil: 6 sheela 5
lecturer: meir komer
discription: the program can calculate the "Symmetric difference" between to numbers-grups.
**************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int set1[6], set2[6], distance[6], different=0,countDifferent=0;

	cout <<"enter first 6 numbers:"<<endl;      // user input the first set of numbers
	for (int i = 0; i < 6; i++)
	{
		cin>>set1[i];
	}

	cout <<"enter next 6 numbers:"<<endl;      // user input the second set of numbers
	for (int i = 0; i < 6; i++)
	{
		cin>>set2[i];
	}

	cout <<"set difference is:"<<endl;
	for (int i = 0; i < 6; i++)
	{

			for (int j=0,k = 0; j < 6; j++,k++)
			{

				if (set1[i]!=set2[j])   // chacking if number in set_1 exsist in set_2
				{	
					different+=1;	//  if number in set_1 in place i, diffrent from  number in set_2 in place j add 1 to different
				}	 	  	 	   	   		   	 	    	 	
			
				if (different==6)		// its mean: this number not exsist at all in set_2 so this number will need to be save to the final cout.
				{	
					distance[k] = set1[i];
					cout<<distance[k]<<" ";
				}
				
			}
			
			countDifferent+=different;    //  this counter neened for case that: all the number in set_1 appers in set_2 .. explian in the next row:

			if (countDifferent==30)   //  if countDifferent=30. its mean  5 * 6 : each nember is differnt only from 5 numbers but its Equal to  one of the numbers 
			{                                  
				cout <<"empty"<<endl;
			}
			different=0;
	}




	//system ("pause");
	return 0;
}
/*******OUTPUT******************
enter first 6 numbers
10 9 7 5 3 1
enter next 6 numbers:
6 5 4 3 2 1
set difference is:
10 9 7
**************************/	 	  	 	   	   		   	 	    	 	

כתיבת תגובה

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