[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Can someone check my work?

#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
biloonline
Asked:
biloonline
  • 6
  • 3
  • 2
  • +2
1 Solution
 
biloonlineAuthor Commented:
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
 
biloonlineAuthor Commented:
This program is due in 45 minutes and thus there 'll be no need to submit your comments afterwards.

Thanks anyway
0
 
ozoCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
biloonlineAuthor Commented:
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
 
sunnycoderCommented:
Add a leading space in front of all scanfs

scanf("%d", &n);

to

scanf(" %d", &n);
          ^
     note the space
0
 
biloonlineAuthor Commented:
Did not do any good.
0
 
ozoCommented:
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
 
biloonlineAuthor Commented:
I don't really understand how to fix that.
0
 
sunnycoderCommented:
>printf("input the coefficients of f\ninput the degree n of the polynomial: ");
                                               ^
                                        remove this \n
0
 
biloonlineAuthor Commented:
That will only make the statement one line... there is no problem in that part of the program. It's the second half.
0
 
sunnycoderCommented:
    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
 
ssnkumarCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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!

  • 6
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now