Solved

Can someone check my work?

Posted on 2004-09-26
16
199 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:Kdo
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now