Politecenico of Torino computer science lab check cinque.

Write a program that acquires two positive values from the user and calculates the Greatest Common Divisor (GCD) using the Euclidean algorithm.
Euclidean algorithm or remains method: it proceeds by successive divisions of the larger number by the smaller, replacing at each step the larger value with the smaller and the smaller with the remainder of the integer division. The process ends when the remainder is 0.

#include<stdio.h>
int main()
{
	int num1, num2, answer;
	printf("pls int the values !\n");
	scanf("%d,%d",&num1, &num2);
	do
	{
		answer = num1 % num2;
		num1 = num2;
		num2 = answer;
		
	} while (answer != 0);
	printf("the value is %d!", num1);
	return 0;
}

Write a program that defines and manipulates an array composed of 10 elements. The program must:
a. acquire values from the keyboard and store them in the array;
通过键盘获取数值并且储存在数组里。
b. print the content of the array at the end of the acquisition;
当获取完成或将所有的数值全部打印在屏幕上。
c. calculate and print the average of the values in the array using a variable of type float;
d. find and print to screen the maximum value and its ordinal position in the array.

#define limit 10
int main()
{
	int arr[10];
	int count, count2;
	int num, max=0, address,reprint;
	float average=0;
	printf("pls int the values !(ten values!)\n");
	for (count = 0; count < limit; count++)
	{
		scanf("%d", &arr[count]);
		if (arr[count]>max)
		{
			max = arr[count];
			address = count;
		}//here is to find the max value and get it's address!
	}
	for (int reprint = 0; reprint < limit;reprint++)
	{

		printf("%d\n", arr[reprint]);

	}
	printf("\n");
	for (count2 = 0; count2 < limit; count2++)
	{
		average = average + arr[count2];
	}
	printf("the average of the values is %d,the maximum value is %d and its ordinal position %d", average / 10, arr[address], address);
	
	return 0;
}

Write a C program that defines two arrays v1 and v2 of N elements (all initialized to 0) of type integer and stores the values, acquired from the keyboard, in the arrays,according to
the following acceptance rules:
a. in v1 store only positive values, and negative values multiples of 3;
b. in v2 store only negative values that are odd and not multiples of 3;
c. all other values are ignored;
d. the insertion ends when one of the two arrays is full, at which point the program prints to the screen the content of the arrays acquired.

#include<stdio.h>
#define limit 10
int main()
{
	int v1[limit] = { 0 };//in v1 store only positive values(ok), and negative values multiples of 3
	int v2[limit] = { 0 };//in v2 store only negative values that are odd and not multiples of 3;
	//all arrays was set to zero!
	int count1=0,count2=0,temp,value;
	do
	{
		printf("pls int the values!\n the value is :");
		scanf("%d", &temp);
		if (temp>0)
		{
			v1[count1] = temp;
			// v1 store positive values
			count1++;
		}
		else if (temp<0)
		{
			if (temp % 3 == 0)
			{
				v1[count1] = temp;
//v1 store negative values multiples of 3
				count1++;
			}
			else if (temp%3!=0 && temp%2!=0)
			{
				v2[count2] = temp;
//v2 store only negative values that are odd and not multiples of 3
				count2++;
			}
			else
			{
				printf("this value is not accept! \n");
//all other values are ignored;
			}	
		}
		else
		{
			printf("zero is not accept!\n");
			continue;
		}
	} while (count1 <= 9 && count2 <= 9);
	printf("input finish! here are the values that we accept!\n");
	for (int  value = 0; value < limit; value++)
	{
		printf("v1:No.%d is %d", value + 1, v1[value]);
		printf("v1:No.%d is %d", value + 1, v2[value]);
		//when one of the two arrays is full, at which point the program prints to the screen the content of the arrays acquired.
	}
	return 0;
}

Write a C program that reads from the keyboard at most N integer values, with N a constant previously defined at will, and save them into an array. The acquisition must proceed until the series of numbers is no longer monotone (i.e., a combination of numbers in ascending or descending order). Print the content of the array at the end of the acquisition.

#include<stdio.h>
#define limit 10
//at most N integer values
int main()
{
	int arr[limit];
	int count=0,temp,value;
	scanf("%d", &arr[count]);
	count++;
	for (count ; count < limit; count++)
	{
		scanf("%d",&arr[count]);

		for (int temp = 0; temp < count; temp++)
		{
			if (arr[count] == arr[temp])
			{
				printf("the %d is not monotony!", arr[temp]);
				return 0;
			}
		}
	}	
	return 0;
}

**Write a C program that analyzes the content of an array containing N positive values and searches for duplicates, N is a constant defined previously. The program must:
a. Acquire the values of the array from keyboard. The acquisition stops when the user enters a negative number, which must not be saved.
b. Start a loop in which it asks for a value, and then save in a second array all the positions in which the value appears in the first array, if present, and then, the program prints the values positions. The loop stops when a negative number is introduced.

{
	int arr1[N], arr2[N];//the first value is use for store the value that you int,and the second value is to store the position!
	int value=0, scan, temp=0;
	int count=0, count2=0,count3=0;//it is use of loop
	for (count; count < N; count++)
	{
		printf("pls int No.%d value!\n the value:", count + 1);
		scanf("%d", &arr1[count]);

		if (arr1[count]<0)
		{
			break;
		}//the only way to brock the loop is arrive the limit or get a negitave value!
	}
	printf("now we will check the value's position!\n ");
	//above here is to store the value (also check for the negitave value!)
	for (count2; count2 < count; count2++)//是否考虑输入的数值为0的情况?
	{
		printf("pls int the values !\nthe value is :");
		scanf("%d",&scan);
		if (scan < 0)
		{
			printf("we get a command line ! we have to stop!");
			return 0;
		}
		for (count3; count3 < count+1; count3++)
		{
			if (scan == arr1[count3])//
			{
				arr2[value] = count3;
				value++;
				printf("the value have appear in the arr1!the position is%d!\n", count3+1);
				//如果有重复的情况会再一次打印出来
			}
		}

	}

	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值