Sie sind auf Seite 1von 14

DS&A EXERCISE

1. Write a program that converts a given text to "Pig Latin". Pig Latin consists of removing
the first letter of each word in a sentence and placing that letter at the end of the word. This
is followed by appending the word with letters "ay".
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void initialize(char english[], char piglatin[]);
void readinput(char english[]);
int countwords(char english[]);
void convert(int words, char english[], char piglatin[]);
void writeoutput(char piglatin[]);
main( )
{
char english[80], piglatin[80];
int words;
printf("\nPig Latin Translator in C\n\n");
printf( "Type \ 'END\' when finished\n\n");
do
{
/* processing a new line of text */
initialize(english, piglatin);
readinput(english);
/* testing stopping condition */
if (toupper(english[0]) == 'E' && toupper(english[1]) == 'N' && toupper(english[2]) ==
'D')
break;
/* count the number of words in the line */
words = countwords(english);
/* Now Pig Latin Translator in C converts English to Pig Latin */
convert(words, english, piglatin);
writeoutput(piglatin);
}
while (words >= 0);
printf("\naveHay aay icenay ayday (Have a nice day)\n");
}
/* initializing character arrays with blank spaces */
void initialize(char english[], char piglatin[])
{
int count;

for (count = 0; count < 80; ++count)


english[count] = piglatin[count] = ' ';
return;
}
/* reading one line of text in English */
void readinput(char english[])
{
int count = 0;
char c;
while (( c = getchar()) != '\n')
{
english[count] = c;
++count;
}
return;
}
/* scanning the given message or line of text and determining the number of words in it */
int countwords(char english[])
{
int count, words = 1;
for (count = 0; count < 79; ++count)
if (english[count] == ' ' && english[count + 1] != ' ')
++words;
return (words);
}
/* now Pig Latin translator in C coverts each word into Pig Latin */
void convert(int words, char english[], char piglatin[])
{
int n, count;
int m1 = 0; /* m1 indicates the position of beginning of each word */
int m2; /* m2 indicates the end of the word */
/* convert each word */
for (n = 1; n <= words; ++n)
{
/* locating the end of the current word */
count = m1 ;
while (english[count] != ' ')
m2 = count++;
/* transposing the first letter of each word and adding 'ay' at the end */
for (count = m1 ; count < m2; ++count)
piglatin[count + (n - 1)] = english[count + 1];
piglatin[m2 + (n - 1)] = english[m1];

piglatin[m2 + n] = 'ay'; /* adding 'ay' at the end */


/* reseting the initial marker */
m1 = m2 + 2;
}
return;
}
/* now Pig Latin translator in C displays the line of English text in Pig Latin */
void writeoutput(char piglatin[])
{
int count = 0;
for (count = 0; count < 80; ++count)
putchar(piglatin[count]);
printf("\n");
return;
}

2. When the number is 64,253 is multiplied by 365 the product is 23,452,345. Notice that the
first four digits are the same as the last four digits (2345 and 2345). Write a program that will
find any and all integers that can be multiplied by 365 to produce an eight-digit product
where the first four digits are the same as the last four digits.
#include<stdio.h>
#include<conio.h>
void main()
{
double a,b,c,temp=0;
int i=7;
int x[8]={1,2,3,4,5,6,7,8};
clrscr();
printf("\n\n Enter any number:");
scanf("%lf",&a);
printf("\n\nx= %lf",a);
printf("\n\ny= %lf",b);
b=a*365;
printf("\n\nb= %lf",b);
if( (b<=9999999)||(b>99999999))
{
printf("\n\n %0.0lf is not 8 digit number",b);
}
else
{
printf("\n\n %0.0lf is 8 digit number",b);
c=b;
i=7;
while(c>0)
{
printf("\n c=%lf temp=%lf i=%d",c,temp,i);
temp=(int)c%10;
x[i]=(int)temp;
c=(int)c/10;
i--;
getch();
}
}
if( (x[0]==x[4])&& (x[1]==x[5]) &&(x[2]==x[6]) && (x[3]==x[7]) )
{
printf("\t %lf is required number.",a);
}
else
{
printf("\t %lf is not required number.",a);
}
getch();
}

3. Given an integer n > 1 and an integer p > 1 you are to write a program that determines the
positive nth root of p. In this problem, given such integers n and p, p will always be of the
form k^n for an integer k ( this integer is what your program much find).
#include<stdio.h>
int main()
{
int n,p,k,var,i;
scanf("%d%d",&n,&p);
for(k=1;k<p;k++)
{
var=k;
for(i=1;i<n;i++)
{
var=var*k;
}
if(var==p)
{
printf("k=%d",k);
}
}
printf("no such integer k");
return 0;
}

4. A company wants to transmit data over the telephone line, but they are concerned that their
lines are tapped. All of their data is transmitted as four-digit integers. They have asked you
to write a program that encrypts their data so that it may be transmitted more securely. Your
program should read a four-digit integer and encrypt it as follows:
Replace each digit by ((digit + 7) mod 10). Then, swap the first digit with the third, swap the
second digit with the fourth, and print the encrypted integer.
#include <iostream.h>
int Encrypt (int value)
{
// compute seperate digits.
// 1234
int First,Second,Third,Forth;
First = value/1000; // 1
Second = (value%1000) / 100; // 2
Third = (value%100) / 10; // 3
Forth = value % 10; // 4
First = (First+7) % 10; // 8
Second = (Second+7) % 10; // 9
Third = (Third+7) % 10; // 0
Forth = (Forth+7) % 10; // 1
if ( Third <= 0 ) { Third = 1; } // if third = zero we get a 3digit back
int Encrypted_number = (Third*1000)+(Forth*100)+(First*10)+(Second); // 1189
return (Encrypted_number);
}
void Printmenu()
{
std::cout <<"[1] To encrypt a 4 digit number" << std::endl;
}
}
int main ()
{
Printmenu();
int to_be_encrypted,encrypted;
std::cout <<"Please enter a 4 digit integer number for encryption\n";
std::cin >> to_be_encrypted;
encrypted = Encrypt(to_be_encrypted);

std::cout <<"The original number: " << to_be_encrypted <<


std::endl;
std::cout <<"The encrypted number: " << encrypted << std::endl;
}
std::cin.get();
std::cin.get();
return 0;
}
5. Given a positive integer as input. If it has exactly three digits,then determine if the integer
equals the sum of the cubes of its digits.
#include <iostream.h>
int main()
{
int n, n1, rem, num=0;
cout<<"Enter a positive integer: ";
cin>> n;
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
cout<<"The integer is equal to the sum of the cubes of its digits.";
else
cout<<"The integer is equal to the sum of the cubes of its digits.";
}

6. "Trimmed Mean" In your account you will find a partially completed program named
TMEAN. It reads an array of integers and displays the array, then quits. You are to complete
the program so it that computes the "trimmed mean" of the array. This means that the largest
and smallest numbers are to be excluded from the calculations. The answer should be written
to 2 decimal places.
#include<stdio.h>
int main()
{
int n,i,j,t;
float a[50],tmean=0;
printf("Enter the size of array");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
t=[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=1;i<n-1;i++)
{
tmean+=a[i];
}

tmean=tmean/(n-2);
printf("Trimmed mean=%f",tmean);
return 0;
}

7. "Number of Digits"
You are to write a complete program, named DIGITS, which should prompt for a variable of
type integer. It can by negative, zero, or positive. The program must then determine how
many digits the integer contains.
#include <stdio.h>
int main()
{
int n,digit=0;
printf("Enter an integer: ");
scanf("%d", &n);
while(n!=0)
{
n/=10;
++digit;
}
printf("Number of digits: %d",digit);
}

8. "Pascal's Triangle"
Your program, named TRIANGLE, is to display N rows of Pascal's Triangle. The program
must prompt for N, then print the triangle so that its peak is at top center. Your program must
work for N from 1 to 10. Spacing must take into account the possibility of numbers of
varying sizes. Leave at least two spaces between the numbers of each row, and double space
the rows.
#include <stdio.h>
long fact(int);
int main()
{
int i, n, c;
printf("Enter the number of rows \n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
printf("%ld ",fact(i)/(fact(c)*fact(i-c)));
printf("\n");
}
return 0;
}
long fact(int n)
{
int c;
long result = 1;

for (c = 1; c <= n; c++)


result = result*c;
return result;
}

9. Input real numbers A and B. Without performing any arithmetic computations, decide
whether the sum of A and B is positive, negative, or zero.
#include<stdio.h>
float sum(float, float);
int main()
{
int a, b,result;
printf("Enter the two Numbers: ");
scanf("%f %f", &a, &b);
add(a,b);
printf("Addition of two num. is : %f", result(a, b));
if (result<0)
printf("The sum of %.2f and %.2f is negative.",result);
else if (num>0)
printf("The sum of %.2f and %.2f is positive.",result);
else
printf("The sum of %.2f and %.2f is zero.");
return 0;
}
float add(float num1, float num2)
{
int i;
for (i = 0; i < num2; i++)
result=num1++;
return num1;
}

10. Consider the following scheme for computing the cost of a direct dial long distance call,
based on the time the call originates, the length of the call, and a basic rate of 107 cents for
the first minute, and 57 cents/minute for all additional minutes.
Time Call Day of
Originates Week Discount 7 am - 5 pm weekday none 5 pm- 11 pm any day 20% 11 pm- 7
am any day 40% 7 am - 11 pm Saturday 20% 7 am- 5 pm Sunday 40%
Write a program which will allow entry of data for the following: day of the week (1 for
Sunday, 2 for Monday, ..., 7 for Saturday); time call originates, in terms of a 24 hour clock
(to calculate length of call).
Your program should print out the cost of a call in cents
Time call originates

Day of week

discount

7am-5pm
5pm-11pm
11pm-7am
7am-11pm
7am-5pm

weekday
Any day
Any day
saturday
Sunday

None
20%
40%
20%
40%

#include<stdio.h>
#include<math.h>
#include<conio.h>
int day;
int time;
float disc=0.0;
int duration;
float cost,r;
void main[]
{
clrscr();
printf(\n Enter the Day of the week);
printf(\n 1- Sunday);
printf(\n 2- Monday);
printf(\n 3- Tuesday);
printf(\n 4- Wednesday);
printf(\n 5- Thursday);
printf(\n 6- Friday);
printf(\n 7- Saturday);
scanf(%d ,&day);
printf(Enter time of calling);
scanf(%d,&time);
printf(Enter duration of calling);
scanf(%d&duration);
if(time>1700&&time<2300)
disc=0.2;
else if(time>2300||time<700)
disc=0.4;

else if((day==7)&&(time>700&&time<2300))
disc=0.2;
else if((day==1)&&(time>700&&time<1700))
disc=0.4;
r=((duration*57)+50)*disc;
cost=(duration*57)+50-r;
printf(%f,cost);
getch();
}

Das könnte Ihnen auch gefallen