PDA

View Full Version : Little C Help?



hybrid101
May 3rd, 2007, 03:14 AM
eh, we got some questions for the comp camp, and we have no idea how o solve some of em. we got 6 questions, two hours, and can't get any to work yet! HELP GUYS!


Problem 1. Friday the 13th
Did you know that if the first day of the month falls on a Sunday, there will surely be a “Friday the 13th”. Now, the problem is simple: given the day of the month, can you tell what day it is?

The first line of the input consists of a single integer, indicating the number of test cases to follow. Each following line describes a test case: a single integer from 1 to 31 indicating the day of the month.

Your program should output the which day of the week (Monday, Tuesday, …) that day falls on. The output format should be “Day d: x”, where d is the day of the month and x is the day of the week. It should also print out “Friday the 13th” if it falls on that day.


Sample input:
4
1
31
27
13

Sample output:
Day 1: Sunday
Day 31: Tuesday
Day 27: Friday
Day 13: Friday the 13th







Problem 2. Reverse Mix
Write a program that takes a set of numbers and prints out the numbers in reverse order, and sum of those numbers. The first line of input indicates the number of lines to follow. Each line contains a single floating-point number. After all numbers have been given, print out the numbers in a single line in reverse order (i.e., xn, xn-1, …, x2, x1) and the sum of those numbers accurate to 2 decimal places.

Sample input:
3
1.2
3.45
6.789

Sample output:
6.789 3.45 1.2 = 11.44







Problem 3. R3@d Th1$
In order to send messages to one another, two geeks Albert and Edison, make a simple cipher out of their messages. The cipher just changes some characters in the message:




Character



Cipher



O *




I 1



L 7




A @




S $




E 3









whitespace _(underscore)









Input starts with a single integer indicating the number of lines to follow. The lines that follow contains words in uppercase, with each line containing only letters. Input will not contain any blank lines.

Your program should output the cipher in the same number of lines as in the input.

Sample input:
2
CHUCK NORRIS DOES NOT SLEEP
HE WAITS

Sample output:
CHUCK_N*RR1$_D*3$_N*T_$733P
H3_W@1T$







Problem 4. Factorial
Write a program that takes a positive integer n (1 <= n <= 25) and print out n!. Your program should keep reading integers until n = 0.

Sample input:
3
8
6
0

Sample output:
3! = 6
8! = 40320
6! = 720







Problem 5. Right Triangle
Write a program that will take a single positive integer n (1 <= n <= 9) as input, and print out a right triangle with the adjacent and opposite edges n units long. Draw the triangle using n as the character.

Sample input:
7

Sample output:
7777777
777777
77777
7777
777
77
7







Problem 6. Multiplication Table
Write a program that gets two positive integers n and m (1 <= n, m <= 10) and prints out a multiplication table n units wide and m units high. Each number must be formatted to print a leading zero if it is < 10. Follow the format of the sample output.

Sample input:
3
6

Sample output:
01 02 03
02 04 06
03 06 09
04 08 12
05 10 15
06 12 18
i swear i'll give a cookie to whoever can help:D THANKS!

foodpk
May 3rd, 2007, 06:28 AM
Alright, here's number 5. I haven't tested it but I think it should work like expected.


#include stdio.h
#include stdlib.h
main (int argc, char* argv[]) {
int i,j,k;
printf("What's the number?\n");
fscanf(stdin, "%d",&i);
for (j=i;j>=1;j--) {
for (k=j;k>=1;k--) {
printf("%d",i);
}
printf("\n");
}
}

foodpk
May 3rd, 2007, 06:43 AM
Because I'm not in Ubuntu right now I can't help you with the others because they'd require a few runs of testing, but here's some tips on how I'd solve them.
1. The 1st starts with a Sunday. So that means that to get, say, which day the 25th is, you would do 25%7, which would result in 4. So that means that it's a wednesday, because if it would be 1, it would be a sunday. If it would be 2, that's a monday, etc.
2. I'd do a while loop and while the number you read with fscanf is not 0, do the following: add to an array of floats, each time use realloc to increase the size of the array, also keep track of how large it is in an integer. Then outside that while loop just iterate down the array and keep adding the elements together in a temporary float, then display them.
3. fscanf for an integer. That's how many times you will iterate through the lins. Then read the line with fscanf(stdin, "%s",yourchar), which will give you a string. Then iterate through that string and check if a character is one of the chars to replace. If it is, output the replaced character, else just output the char.
4. Also use an array and realloc each time it's added within the while loop. After it's done, iterate through the array and output a factorial of each number. 4! equals 4*3*2*1 so you'll probably know how to deal with that.
6. This one's pretty easy, I'm sure you'll be able to handle it :D

TheColonial
May 3rd, 2007, 07:57 AM
Surely the idea of the exercise is to write the code yourself (or try to) and learn something instead of having someone else do the hard work for you? I don't believe in spoonfeeding, so I won't be doing the work for you. Instead I have some other questions:

What have you tried already?
Where have you got stuck?
What bits don't you understand?

Answer those and I'll help you learn how to solve them yourself.

hybrid101
May 3rd, 2007, 11:46 PM
thanks guys, we got 4, lol. 1 in one hour, 3 more in the last hour:D 3 was HARD!