Solved

Can someone check my work?

Posted on 2004-09-26
16
242 Views
Last Modified: 2010-04-15
#include <stdio.h>

const capacity = 101;

char peek();
void InputCoefficients(int*mPtr, float c[]);
void OutputCoefficients(int m, const float c[]);
void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]);
void ReduceDegree(int* mPtr, float c[]);

int main(void)
{
       int m, n;
       float f[capacity], g[capacity], h[capacity];
       printf("input the coefficients of f\ninput the degree n of the polynomial: ");
       scanf("%d", &n);
       printf("Input the polynomial coefficients c[0], ..., c[n]: ");
       InputCoefficients(&n, f);
       getchar();
       printf("input the coefficients of g\ninput the degree m of the polynomial: ");
       scanf("%d", &m);
       printf("Input the polynomial coefficients c[0], ..., c[m]: ");
       InputCoefficients(&m, g);
       getchar();
       printf("\nthe sum f + g equals\n\n");
       /*Sum(sum);*/
       printf("\nthe product f * g equals\n\n");
       Product(m, f, n, g, &m, h);
       return 0;
}

char peek(){
       char c = getchar();
       ungetc(c, stdin);
       return c; }

void ReduceDegree(int* mPtr, float c[]){
       while (*mPtr >= 1 && c[*mPtr] == 0)
               (*mPtr)--; }

void InputCoefficients(int*mPtr, float c[]){
       *mPtr=0;
       while (peek() != '\n'){
               scanf("%f", &c[*mPtr]);
               *mPtr += 1; }
       if (*mPtr != 0)
               *mPtr -= 1;
       else c[0] = 0;
       ReduceDegree(mPtr, c);
       getchar(); }

void OutputCoefficients(int m, const float c[]){
       int i;
       for (i = 0; i <= m; i++)
               printf("%.1f ", c[i]);
       printf("\n"); }

/*void Sum(){
       int i, j;
       for (i=0; i<=n; i++){
               */

void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]){
       int i, j;
       *kPtr = m+n;
       for (i=0; i<=*kPtr; i++)
               h[i]=0;
       for (i=0; i<=m; i++)
               for (j=0; j<=m; j++)
                       h[i+j] += f[i]*g[j]; }


That what's supposed to output:

input the coefficients of f
input the degree n of the polynomial: 3
Input the polynomial coefficients c[0], ..., c[n]: 1 3 3 1
input the coefficients of g
input the degree n of the polynomial: 1
Input the polynomial coefficients c[0], ..., c[n]: 1 1
the sum f + g equals
[0] = 2.000000
[1] = 4.000000
[2] = 3.000000
[3] = 1.000000
the product f * g equals
[0] = 1.000000
[1] = 4.000000
[2] = 6.000000
[3] = 4.000000
[4] = 1.000000


Thanks
0
Comment
Question by:biloonline
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +2
16 Comments
 

Author Comment

by:biloonline
ID: 12157358
It's not even asking to input the degree for the second polynomial. Also it's missing the sum function, can someone please assist on writting that function?

Thanks
0
 

Author Comment

by:biloonline
ID: 12157487
This program is due in 45 minutes and thus there 'll be no need to submit your comments afterwards.

Thanks anyway
0
 
LVL 84

Expert Comment

by:ozo
ID: 12157511
void InputCoefficients(int*mPtr, float c[]) ends with a getchar() and the call is also followed by a getchar()  Do you need them both?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:biloonline
ID: 12157519
Well, I didn't think so either, but the problem was in compiling. The prompt to input the degree of the first polynomial wasn't even coming up, but instead skipping directly to the prompt for the coefficients.

I added the getchar after the call, so it asked for the degree of the first polynomial. Now it's not asking for the degree of the second one, but skipping to asking about the coefficients.

I don't know why.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157557
Add a leading space in front of all scanfs

scanf("%d", &n);

to

scanf(" %d", &n);
          ^
     note the space
0
 

Author Comment

by:biloonline
ID: 12157567
Did not do any good.
0
 
LVL 84

Expert Comment

by:ozo
ID: 12157576
You read  &n and then InputCoefficients immediately sets it to 0
the peek() != '\n' is probably reading the '\n' you typed after scanf("%d", &n);
0
 

Author Comment

by:biloonline
ID: 12157592
I don't really understand how to fix that.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157599
>printf("input the coefficients of f\ninput the degree n of the polynomial: ");
                                               ^
                                        remove this \n
0
 

Author Comment

by:biloonline
ID: 12157611
That will only make the statement one line... there is no problem in that part of the program. It's the second half.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12157663
    InputCoefficients(&n, f);
>>     fscanf(stdin,"%*[^\n]",(char *)f);  << add this line
     printf("input the coefficients of g\ninput the degree m of the polynomial: ");
0
 
LVL 8

Accepted Solution

by:
ssnkumar earned 250 total points
ID: 12157817
Hi,

   I have modified your code a little bit. It is taking inputs now.
   The buffering is the problem. After scanf(), there will be a '\n' character remaining in the buffer, which has to be read, if you want to make use of getchar().

   Hope it helps.

-ssnkumar

Here is the corrected code:

#include <stdio.h>

const int capacity = 101;

char peek();
void InputCoefficients(int*mPtr, float c[]);
void OutputCoefficients(int m, const float c[]);
void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]);
void ReduceDegree(int* mPtr, float c[]);

int main(void)
{
       int m, n;
       float f[capacity], g[capacity], h[capacity];

       printf("input the coefficients of f\ninput the degree n of the polynomial: ");
       scanf("%d", &n);
       getchar();
       printf("Input the polynomial coefficients c[0], ..., c[n]: ");
       InputCoefficients(&n, f);
//       getchar();
       printf("input the coefficients of g\ninput the degree m of the polynomial: ");
       scanf("%d", &m);
       getchar();
       printf("Input the polynomial coefficients c[0], ..., c[m]: ");
       InputCoefficients(&m, g);
//       getchar();
       printf("\nthe sum f + g equals\n\n");
       /*Sum(sum);*/
       printf("\nthe product f * g equals\n\n");
       Product(m, f, n, g, &m, h);
       return 0;
}

char peek(){
       char c = getchar();
       if (c != '\n') ungetc(c, stdin);
       return c; }

void ReduceDegree(int* mPtr, float c[]){
       while (*mPtr >= 1 && c[*mPtr] == 0)
               (*mPtr)--; }

void InputCoefficients(int*mPtr, float c[]){
       *mPtr=0;
       while (peek() != '\n'){
               scanf("%f", &c[*mPtr]);
               *mPtr += 1; }
       if (*mPtr != 0)
               *mPtr -= 1;
       else c[0] = 0;
       ReduceDegree(mPtr, c);}
//       getchar(); }

void OutputCoefficients(int m, const float c[]){
       int i;
       for (i = 0; i <= m; i++)
               printf("%.1f ", c[i]);
       printf("\n"); }

/*void Sum(){
       int i, j;
       for (i=0; i<=n; i++){
               */

void Product(int m, const float f[], int n, const float g[], int
*kPtr, float h[]){
       int i, j;
       *kPtr = m+n;
       for (i=0; i<=*kPtr; i++)
               h[i]=0;
       for (i=0; i<=m; i++)
               for (j=0; j<=m; j++)
                       h[i+j] += f[i]*g[j]; }
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 12174142

Hi Sunny,

I'm certainly glad that you made the suggestion to add the fscanf() statement with the enhanced format string.  I was thinking just a short time ago that the board sees enough scanf() issues because the NewLine character remains buffered that the standard answer should be to use the correct format string, not the throw in getchar() statements to skip the NewLine.

Go forth and multiply, or in your case, "do long division".
Kent


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question