stackoverflow1453

Programming&&Games&&Life

Archive for February, 2013

Write a C program that calculates Pythagorean Triples nonrecurring

/*
	Name:
	Question Copyright: C How to Program-Deitel
	Author: StackOverflow1453
	Date: 27.02.2013 15:35:11
	Description:
		(Pythagorean Triples) A right triangle can have sides that are all 
		integers. The set of three integer values for the sides of aright 
		triangle is called a Pythagorean triple. These three sides must satisfy 
		the relationship that the sum of the squares of two of thesides is equal 
		to the square of the hypotenuse. Find all Pythagorean triples for 
		side1, side2, and the hypotenuse all no larger than500. Use a 
		triple-nested for loop that simply tries all possibilities. This is an 
		example of “brute force” computing. It is not aesthet-ically pleasing 
		to many people. But there are many reasons why these techniques are 
		important. First, with computing power in-creasing at such a phenomenal 
		pace, solutions that would have taken years or even centuries of computer 
		time to produce with thetechnology of just a few years ago can now be 
		produced in hours, minutes or even seconds. Recent microprocessor chips 
		can processa billion instructions per second! Second, as you will learn 
		in more advanced computer science courses, there are large numbers of 
		interesting problems for which there is no known algorithmic approach 
		other than sheer brute force. We investigate many kinds ofproblem-solving
		methodologies in this book. We will consider many brute force approaches 
		to various interesting problems.
*/

#include <stdio.h>

int main(void){
	
	int side1=1;
	int side2=1;
	int hypotenus=1;
	int till;
	int count=0;
	
	printf("Till what number do you want to find triplets?");
	scanf("%d",&till);	
	
	for(side1=1;side1<=till;side1++){
		
		for(side2=side1;side2<=till;side2++){  //side2=side1 need to filter duplicate triplets!!! 
			
			for(hypotenus=1;hypotenus<=till;hypotenus++){
				
				if((hypotenus*hypotenus==side1*side1+side2*side2)){ 
				
					
					count++;
					printf("%5d %5d %5d is a triple \n",side1,side2,hypotenus);
				}
			}
				
		}
			
	}
	printf("\n");
	printf("%d triplets found.",count);
	return 0;
	
}

“error LNK2019: unresolved external symbol _WinMain@16 referenced in function” error in Visual Studio Express 2012

Error

Right Click your Project
Go to “Linker settings
System”.
Change the field “Subsystem” from “Windows” to “Console”.
Build Again
Done.

Calculate the value of pi from the infinite series by using pi=4/1-4/3+4/5-4/7…

There area some things I learned from this program:

1- Dev-C++ does not support long double type printf as
printf("Accuracy: %8d , Pi=%12Lf\n",i+1,pi);

so I had to change it to double before printing as follows.

printf("Accuracy: %8d , Pi=%12lf\n",i+1,(double)pi);

2- Visual Studio Express 2012 compiled code with Long Double well.

Program change is not needed. Visual Studio 12 interface seems very good. Yes I am a beginner!

/*
	Name:
	Question Copyright: C How to Program-Deitel
	Author: StackOverflow1453
	Date: 27.02.2013 11:33:53
	Description:Calculate the value of ? from the infinite series 
	pi=4/1-4/3+4/5-4/7...
*/

#include <stdio.h>

int main(void){
	
	long double pi=4.00000;
	int accuracy;
	int i=1;
	int counter=1;
	
	printf("Enter accuracy:");
	scanf("%d",&accuracy);
	if (accuracy==1)
	pi=4;
	
	else {
	
		printf("Accuracy: %8d , Pi=%12lf\n",1,4.000000);
		for(;i<accuracy;i++){
			
			if (i%2==0)
			pi=pi+4.0/(counter+2.0);
			else 
			pi=pi-4.0/(counter+2.0);
			counter+=2;
			
			printf("Accuracy: %8d , Pi=%12Lf\n",i+1,pi);
		
		}
	}
	getch();
	return 0;
}

Write a program which calculates and displays the total retail value of all products sold last week.

/*
	Name:
	Question Copyright: C How to Program-Deitel
	Author: StackOverflow1453
	Date: 27.02.2013 10:32:26
	Description: A mail order house sells five different products whose 
	retail prices are shown in the following table:
		
		Write a program that reads a series of pairs of numbers as follows:
		a)Product number
		b)Quantity sold for one day
		
		Your program should use a switch statement to help determine the retail 
		price for each product. Your program should calculate and display the total 
		retail value of all products sold last week.
		
		Product number		Retail price
		1						$ 2.98
		2						$ 4.50
		3 						$ 9.98
		4 						$ 4.49
		5 						$ 6.87
		
		Output:
			Enter pairs of item numbers and quantities.
			Enter -1 for the item number to end input.
			1 1
			2 1
			3 1
			4 1
			5 1
			6 1
			Invalid product code:  6            
			Quantity:  1
			1 1
			-1
			The total retail value was:  31.80
		
*/

#include <stdio.h>

int main(void){
	
	int type=0;
	int quantity;
	double total=0;
	
	printf("Enter pairs of item numbers and quantities.\n");
	printf("Enter -1 for the item number to end input.\n");
	
	
	while(type!=-1){
		
		scanf("%d %d",&type,&quantity);
		
		switch(type){
			
			case 1:
				total=total+quantity*2.98;
				break;
			case 2:
				total=total+quantity*4.50;
				break;
			case 3:
				total=total+quantity*9.98;
				break;
			case 4:
				total=total+quantity*4.49;
				break;
			case 5:
				total=total+quantity*6.87;
				break;
			default: 
				printf("type is wrong:try again\n");
				break;
				
		}
		
		
	}
	printf("Sum of the purchases is :%lf",total);
}

Write a program that reads five numbers (each between 1 and 30). For each number read, your program should print a line containing that number of adjacent asterisks.

Interesting thing about this program is if you don’t know what is STDIN and how scanf works, program is hard to understand. I asked this in stackoverflow. It is better if you study the below post in conjunction with this program.

http://stackoverflow.com/questions/15091381/why-is-this-how-number-variable-gets-5-different-values-how-does-it-remember

/*
	Name:
	Copyright:
	Author: StackOverflow1453
	Date: 26.02.2013 15:56:43
	Description:
	One interesting application of computers is drawing graphs and bar charts 
	(sometimes called “histograms”). Write a program that reads five numbers 
	(each between 1 and 30). For each number read, your program should print a 
	line containing that number of adjacent asterisks. 
	For example, if your program reads the number seven, it should print *******.
	
	Program output:
		
		Enter 5 numbers between 1 and 30: 28 5 13 24 7
		****************************
		*****
		*************
		************************
		*******
*/

#include <stdio.h>

int main(void){
	
	int number;
	int i;
	int j;
	
	printf("Enter 5 numbers between 1 and 30:");
	
	for(i=1;i<=5;i++){
		
		scanf("%d",&number);
		
		for (j=1;j<=number;j++)
			printf("*");
		printf("\n");
		
	}
	
	
}

Collecting money becomes increasingly difficult during periods of recession, so companies may tighten their credit limitsto prevent their accounts receivable (money owed to them) from becoming too large. In response to a prolonged recession, one com-pany has cut its customer’s credit limits in half. Thus, if a particular customer had a credit limit of $2000, this customer’s credit limitis now $1000. If a customer had a credit limit of $5000, this customer’s credit limit is now $2500. Write a program that analyzesthe credit status of three customers of this company. For each customer you are given:a)The customer’s account numberb) The customer’s credit limit before the recessionc)The customer’s current balance (i.e., the amount the customer owes the company).Your program should calculate and print the new credit limit for each customer and should determine (and print) which cus-tomers have current balances that exceed their new credit limits. Program Output: Enter account, limit, balance: 100 4000.00 2136.87 New credit limit for account 100 is 2000.00 Limit exceeded for account 100 Enter account, limit, balance: 200 10500.00 4927.39 New credit limit for account 200 is 5250.00 Enter account, limit, balance: 300 1000.00 750.00 New credit limit for account 300 is 500.00 Limit exceeded for account 300

/*
	Name:
	Copyright:
	Author: StackOverflow1453
	Date: 26.02.2013 15:45:14
	Description: Collecting money becomes increasingly difficult during periods 
	of recession, so companies may tighten their credit limitsto prevent their 
	accounts receivable (money owed to them) from becoming too large. 
	In response to a prolonged recession, one com-pany has cut its customer's 
	credit limits in half. Thus, if a particular customer had a credit limit of 
	$2000, this customer’s credit limitis now $1000. If a customer had a credit 
	limit of $5000, this customer's credit limit is now $2500. Write a program 
	that analyzesthe credit status of three customers of this company. 
	For each customer you are given:a)The customer’s account numberb)
	The customer’s credit limit before the recessionc)The customer’s current 
	balance (i.e., the amount the customer owes the company).Your program should 
	calculate and print the new credit limit for each customer and should 
	determine (and print) which cus-tomers have current balances that 
	exceed their new credit limits.
	
	Program Output:
		Enter account, limit, balance: 100 4000.00 2136.87
		New credit limit for account 100 is 2000.00
		Limit exceeded for account 100
	
		Enter account, limit, balance: 200 10500.00 4927.39
		New credit limit for account 200 is 5250.00
		
		Enter account, limit, balance: 300 1000.00 750.00
		New credit limit for account 300 is 500.00
		Limit exceeded for account 300
		
*/

#include <stdio.h>

int main(void){
	
	int account;
	float limit;
	float balance;
	
	printf("Enter account, limit, balance:");
	scanf("%d %f %f",&account,&limit,&balance);
	
	printf("New credit limit for account %d is %.2f. \n",account, limit/2);
	
	if(limit/2<balance)
		printf("Limit exceeded for account %d .\n",account);
		
	return 0;
	
}

Write a program that prints the following patterns separately one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single printf statement of the form printf( “*” ); (this causes the asterisks to print side by side). Hint: The last two patterns require that each line begin with an appropriate number of blanks.]

/*
	Name:
	Copyright:
	Author: StackOverflow1453
	Date: 26.02.2013 14:47:58
	Description:
	Write a program that prints the following patterns separately one below 
	the other. Use for loops to generate the patterns. All asterisks (*) should 
	be printed by a single printf statement of the form printf( "*" ); (this 
	causes the asterisks to print side by side). Hint: The last two patterns 
	require that each line begin with an appropriate number of blanks.]
	
	*			*******		*******		          *
	**			******		 ******		         **
	***			*****		  *****	                ***
	****			****		   ****		       ****
	*****			***		    ***		      *****
	******		        **		     **		     ******
	*******		        *  		      *		    *******
	fig a			fig b	          fig c	             fig d
*/

#include <stdio.h>

int main(void){
	
	int row;
	int column;
	// fig a :
	for (row=1;row<=7;row++){
			
			for  (column=1;column<=row;column++)
					printf("*");
					printf("\n");
			
	}
	printf("\n");
	//fig b
	for (row=1;row<=7;row++){
			
			for  (column=7;column>=row;column--)
					printf("*");
					printf("\n");
	
	}
	printf("\n");
// fig c :
	for (row=1;row<=7;row++){
			
			for  (column=1;column<=row-1;column++)
					printf(" ");
			for  (column=1;column<=8-row;column++)
					printf("*");
			printf("\n");
		
	}
	printf("\n");
// fig d :
	for (row=1;row<=7;row++){
			
			for  (column=1;column<=7-row;column++)
					printf(" ");
			for  (column=1;column<=row;column++)
					printf("*");
			printf("\n");
		
	}
	printf("\n");

return 0;
}