- Write a program to print-out geometrical shapes. You must first read a character specifying the shape, and then read the numbers specifying the shape. You must have a function to draw each of these shapes. See the sample-run below.
which shape ? (s)quare, (r)ectangle, (t)riangle : s
enter the length of a side of the square : 6
******
******
******
******
******
******
would you like to continue (y/n) ? y
which shape ? (s)quare, (r)ectangle, (t)riangle : r
enter height and width of the rectangle : 6 8
********
********
********
********
********
********
would you like to continue (y/n) ? y
which shape ? (s)quare, (r)ectangle, (t)riangle : t
enter the height of the triangle : 7
*
***
*****
*******
*********
***********
*************
would you like to continue (y/n) ? n
/* solution by: eda pinar atasoy */
#include <stdio.h>
/* globals */
int i,j,k,side,x,y,h;
int blank,star;
char c,again;
/* function declerations */
void square();
void rect();
void tri();
int main()
{
start:
printf("which shape ? (s)quare, (r)ectangle, (t)riangle : ");
scanf("%s", &c);
if(c=='s')
square();
else if(c=='r')
rect();
else if(c=='t')
tri();
printf("would you like to continue ? ");
scanf("%s", &again);
if(again=='y')
goto start;
return 0;
}
/* functions */
void square()
{
printf("enter the length of a side of the square : ");
scanf("%d", &side);
for(i=0; i<side; i++)
{
for(j=0; j<side; j++)
printf("*");
printf("\n");
}
}
void rect()
{
printf("enter height and width of the rectangle : ");
scanf("%d %d", &y, &x);
for(i=0; i<y; i++)
{
for(j=0; j<x; j++)
printf("*");
printf("\n");
}
}
void tri()
{
printf("enter the height of the triangle : ");
scanf("%d", &h);
blank=h-1;
star=1;
for(i=0; i<h; i++)
{
for(j=0; j<blank; j++)
printf(" ");
for(k=0; k<star; k++)
printf("*");
printf("\n");
star += 2;
blank--;
}
}
-
Write a program that prompts the user to enter a series of characters from the keyboard terminated by the newline character ('\n').
This program will output a summation of how many upper case letters, how many lower case letters, how many blank spaces and
how many other characters were encountered and the total number of characters.
Sample-run:
enter the input string: This is JUst a Sample Run of the stupid homeWork.
Upper case letters : 6
Lower case letters : 33
Blank spaces : 9
Other characters : 1
Grand total : 49
Notes:
- Using the getchar() function in the conditional expression for the while loop will simplify your life:
while( (letter = getchar()) != '\n' )
- Lower case letters are within a range of characters ('a' to 'z'), as are upper case letters ('A' to 'Z'). Do not write a gigantic switch statement with a case for each lower case letter and a case for each upper case letter.
/*solution by: Mert Yetkin*/
#include<stdio.h>
int a,up=0,low=0,blank=0,other=0,tot=0;
void main()
{
char a;
while((a = getchar()) !='\n'){
if(a>='A' && a<='Z') up++;
else if(a>='a' && a<='z') low++;
else if(a== ' ') blank++;
else other++;
tot++;
}
printf("upper case letters=%d\nlower case letters=%d\nblank characters=%d\nother characters=%d\ngrand total=%d\n",up,low,blank,other-1,tot-1);
}
-
Write a function that computes the circumference of a circle, given its radius. Write another function that computes the surface area of a circle, given its DIAMETER.
Also write the main function that gets the radius as input, uses your functions to compute the circumference and area, and prints the circumference&area. Use #define directive to define PI = 3.13159.
/*solution by: Berkin Ugurlu */
#include <stdio.h>
#define PI 3.13159
float circum(int r){
return 2*PI*r;
}
float area(int d){
return PI*d*d/4;
}
int main ()
{
int r;
printf("Enter radius: ");
scanf("%d",&r);
printf("Circumference =%f\n",circum(r));
printf("Area =%f\n",area(2*r));
return 0;
}
-
Write a function that computes takes 5 floating point arguments and computes the average and standard deviation (STD); prints out the average and returns the standard deviation.
Also write a main program that gets the five values as input and uses the function to compute the STD. Recall that the standard deviation is the square-root of the sum of the squared differences between the values and the mean divided by the number of values.
/*by: Evgueni Stepanov*/
#include <stdio.h>
#include <math.h>
int main ()
{
float num1, num2, num3, num4, num5;
double std;
double stdf(float, float, float, float, float);
printf("Please enter 5 numbers: ");
scanf("%f %f %f %f %f", &num1, &num2, &num3, &num4, &num5);
std = stdf(num1, num2, num3, num4, num5);
printf("Standard deviation:\t%f\n", std);
system("PAUSE");
return 0;
}
double stdf(float num1, float num2, float num3, float num4, float num5)
{
double std, average, x;
average = (num1 + num2 + num3 + num4 + num5)/5;
printf("Average :\t\t%lf\n", average);
x = ((num1 - average) * (num1 - average) +
(num2 - average) * (num2 - average) +
(num3 - average) * (num3 - average) +
(num4 - average) * (num4 - average) +
(num5 - average) * (num5 - average))/5;
std = sqrt(x);
return std;
}
-
Print out a 10x10 multiplication table.
Sample output:
1 2 3 4 5 6 7 8 9 10
== == == == == == == == == ==
1 | 1
2 | 2 4
3 | 3 6 9
4 | 4 8 12 16
5 | 5 10 15 20 25
6 | 6 12 18 24 30 36
7 | 7 14 21 28 35 42 49
8 | 8 16 24 32 40 48 56 64
9 | 9 18 27 36 45 54 63 72 81
10| 10 20 30 40 50 60 70 80 90 100
/* by Ahmet SACAN */
#include <stdio.h>
void NewLine(){
printf("\n");
}
void main(){
int r,c;
printf(" ");
for(c=1; c<=10; c++)
printf("%d ", c);
NewLine();
printf(" ");
for(c=1; c<=10; c++)
printf("== ");
NewLine();
for(r=1; r<=10; r++){
printf("%2d | ", r);
for(c=1; c<=r; c++)
printf("%-4d", r*c);
NewLine();
}
}
-
Write a function that randomly selects a card from a poker deck.
- There are 13 cards: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, and A
- The J, Q, and K have a numeric value of 10.
- The A has a numeric value of 11.
- Each card has an equal probability of being selected.
The function should:
- Print the randomly selected card.
- Return the numeric value of the card as an integer variable
Write a main program to do the following:
- Use the functions time and srand to seed the random number generator.
- Use the function above to generate 2 random cards
- Add up the two numeric values returned by the functions and print their sum.
/*by Ahmet SACAN
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const char SUITS[4][12] = {"hearts", "diamonds", "clubs", "spades"};
const char CARDS[13][8] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "Jack", "Queen", "King"};
int SelectCard(){
int num, suit;
suit = rand()%4;
num = rand()%13;
printf("%s of %s\n", CARDS[num], SUITS[suit]);
return num == 0 ? 11 : num >= 9 ? 10 : num+1;
}
void main(){
int x,y;
srand((unsigned)time(NULL));
x = SelectCard();
y = SelectCard();
printf("your total is: %d\n", x+y);
}