Improve company productivity with a Business Account.Sign Up

x
?
Solved

Can someone check my work?

Posted on 2004-09-26
16
Medium Priority
?
283 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
13 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 85

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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 

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 85

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 1000 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 46

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

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

608 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