Google Answers Logo
View Question
 
Q: C programming problem ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: C programming problem
Category: Computers > Programming
Asked by: mandragora20-ga
List Price: $50.00
Posted: 15 Sep 2004 18:17 PDT
Expires: 15 Oct 2004 18:17 PDT
Question ID: 401790
I need to write a program that prints a table with each line giving an
integer, its square, and its cube. The program will ask the user to
input the lower and upper limits for the table. I will use a for loop
and include at least one user defined function. I can write the
program without the user defined function no problem. I just don't
know how to incorporate the UDF into the program. Can you give me an
example?

Clarification of Question by mandragora20-ga on 15 Sep 2004 20:17 PDT
Got it thanks. Yes it is a homework question, but was not sure of the
format and where to put in the program. I used long square(long x) and
long cube(long y). After inserting the two functions it seemed to
compile and run like it did before I put it in. I still don't know if
I wrote all elements of the function correctly but I will go with it.
Thanks for the help.
Answer  
Subject: Re: C programming problem
Answered By: tox-ga on 15 Sep 2004 20:27 PDT
Rated:5 out of 5 stars
 
mandragora20-ga,

A good example of using user defined functions in such a program would
be the following (actually, two user defined functions are used):

---------- BEGIN PROGRAM ----------

#include <stdio.h>

/* Prints a single line of the table */
void printTableLine (int current) {
  printf(" %10d | %10d | %10d \n", current, current*current,
    current*current*current);
}

/* Prints the table header and contents */
void printTable (int lower, int upper) {
  int i = 0;

  printf(" Integer    | Square     | Cube       \n");
  printf("------------|------------|------------\n");

  for (i = lower; i <= upper; i++) {
    printTableLine(i);
  }
}

int main (void) {
  /* Variable declaration */
  int lowerlimit = 0, upperlimit = 0;

  /* Retrieve upper and lower bounds; ensure that the lower bound
     is less than or equal to the upper bound */
  do {
    printf("Please enter the lower limit of the table: ");
    scanf("%d", &lowerlimit);
    printf("Please enter the uppper limit of the table: ");
    scanf("%d", &upperlimit);
  } while (lowerlimit > upperlimit);

  /* Print table  */
  printTable(lowerlimit, upperlimit);
}

---------- END PROGRAM ----------

The two user defined functions may seem pointless, but they serve to
seperate the program into logical components. Certainly, as you
discovered, you can write the program without these functions, but
compartmentalization in this manner makes it clear what each chunk of
code is doing. The benefits of this become much more noticeable when
the program increases in complexity.

If you have any questions or are unsatisfied with the answer, feel
free to ask for clarification.

Cheers,
tox-ga
mandragora20-ga rated this answer:5 out of 5 stars
Thanks, I typed this in and it worked. I will study it to understand
better how user defined functions fit into the program.

Comments  
Subject: Re: C programming problem
From: gopman-ga on 15 Sep 2004 19:06 PDT
 
Sounds like a homework problem... Why not write two functions? Square
and Cube. Something like "long square(long x)" which returns the
square of the argument.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy