Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


C assignment. Please help!

RageBoXxRageBoXx Member Posts: 5
Hi all. I'm a n00b to programming. And I just started C in college. Unfortunately, the dude who's teaching us is going way too fast, and I don't understand anything he's teaching. He gave us an assignment a week after we started learning, and I can't do it. I've tried, I just can't seem to get any of the programs to work. Here are the questions, please, please help me out!

1) Write a program that will count from 1 to 12 and print the count, and its square, for each
1 1
2 4
3 9

2) Write a program that counts from 1 to 12 and prints the count and its inversion to 5 decimal
places for each count. This will require a floating point number.
1 1.00000
2 .50000
3 .33333
4 .25000

3) Write a function that calculates the average of an array of 10 floating-point values and returns the result.

4) Prime numbers can be generated by an algorithm known as the Sieve of Erastosthenes. The algorithm for this procedure is presented here. Write a program that implements this algorithm. Have the program find all prime numbers up to 150.

Step 1. Define an array of integers P. Set all elements Pi to 0, 2 <= i <= n.

Step 2. Set i to 2.

Step 3. If i > n, the algorithm terminates.

Step 4. If Pi is 0, i is prime.

Step 5. For all positive integer values of j, such that i x j < n, set Pixj to 1.

Step 6. Add 1 to i and go to step 3.

5) Based on the following definitions
float f = 1.00;
short int i = 100;
long int l = 500L;
double d = 15.00;
and the seven steps outlined in this chapter for the conversion of operands in expressions, determine the type and value of the following expressions:
f + i
l / d
i / l + f
l * i
f / 2
i / (d + f)
l / (i * 2.0)
l + i / (double) l

6) Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display the following:

[b]nine three two[/b]

7) The factorial of an integer n, written n!, is the product of the consecutive integers 1 through n. For example, 5 factorial is calculated as follows:

5! = 5 x 4 x 3 x 2 x 1 = 1

Write a program to generate and print a table of the first 10 factorials.

8) Write a program that asks the user to type in two integer values at the terminal. Test these two numbers to determine whether the first is evenly divisible by the second, and then display an appropriate message at the terminal.

I seem to be able to do the 8th one. But the rest, I'm stuck. Please help!


  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]First thing comes to mind: FIRE THE TEACHER!
    Or at least tell your student counselor that he is too fast.[/color]
    int i;
    double inverted;
    double average, sum;

    double fvector [] =
    4.224, 5.77, 3.21,
    5.66, 3.096, 2.9942,
    5.827, 3.908, 4.4444,

    [color=Green]// #1:[/color]

    for (i=1; i<=12; i++)
    printf ("%4d %4d
    ", i, i*i);

    [color=Green]// #2:[/color]

    for (i=1; i<=12; i++)
    inverted = 1. / i;
    printf ("%4d %8.5f
    ", i, inverted);

    [color=Green]// #3:[/color]

    for (i=0; i<10; i++)
    sum += fvector [i];
    printf ("AVERAGE: %f
    ", sum / 10.);

    [color=Green]// #6:[/color]

    int entered = 6729; [color=Green]// <-- input it from console[/color]
    int digits [16];
    int numdigits = 0;

    char* sdigvect [] =
    "zero", "one", "two", "three", "four",
    "five", "six", "seven", "eight", "nine"

    while (1)
    digits [numdigits++] = entered % 10;
    entered /= 10;
    if (entered == 0) break;

    for (i=numdigits-1; i>=0; --i)
    printf ("%s ", sdigvect [i]);
    printf ("

    #5: When using values in formulas - the following rules must be taken into

    1. If the float point operand is present as one of the two operands then
    result of such operation is a float point - i.e. it has float point type
    and value.

    int i=3;
    double a=10.0;
    double a2 = a/i;
    printf ("

    %f", a2); // Should be 3.333333333333

    2. (Comes from #1) If both opeands are integer, then the result is an integer.

    int i=3;
    int a=10;
    int a2 = a/i;
    printf ("

    %d", a2); // Should be 3 (remainder is dropped)

    3. Order of operators (if no brackets are present in code):
    - first multiplication and division
    - next addition and subtraction

    a + l/i - f

    1st: l/i
    2nd: a + result of 1st
    3rd: result of 2nd - f
  • bilderbikkelbilderbikkel Member Posts: 754
    : First thing comes to mind: FIRE THE TEACHER!

    Blimey, all those exercise answers can be Googled. Your proposed action seems IMHO too harsh... And what is the point in solving other's people homework? Is your opinion they will become better programmers that way?

    I look forward to a reply, Bilderbikkel
  • uxkxoxnuxkxoxn malaysiaMember Posts: 1

    whats the answer for question 1 and 2???

  • Malcolm_McLeanMalcolm_McLean Member Posts: 254 ✭✭
    edited December 2014

    Here's hello world``

        #include <stdio.h>
        int main(void)
          printf("Hello world\n");
          return 0;

    First thing is to get that up and running. This is often a difficult step.

    Now we've got a compiler. Change the message to something else,
    and prove we're in business.

    Next thing

    #include <stdio.h> 
    int main(void)
      int x;
      x = 2 + 2;
      printf("x = %d\n", x);
      return 0;

    get that working. Change the expression. Now we've got an expression

    Last of all

    int main(void)
       int x;
       int i;
       x = 0;
          x = x + i;
          printf("i %d x %d\n", x, i);
       return 0;

    Now we've got a loop running. x is keeping a cumulative total of the
    integer for 0 to 9 inclusive.

    Now modify that program to achieve your assignments.

Sign In or Register to comment.