-2

I have an error on line 41: expected expression before 'int' line 45: suggest parentheses around comparison in operand of '&'

Also I wanted to make sure if the code from 43-45 made sense.

I am posting on this forum for the first time and I am new to C so please bear with the amateur nature of this post

#include<stdio.h>
#include<math.h>
int main(void)

{
 // LOCAL DECLARATIONS

 int bricks; //the number of bricks available
 int spheres; //the number of spheres available
 int prisms; //the number of prisms available
 int final_string(int bricks, int spheres, int prisms); //the longest possible string with the given shapes in an alternating fashion

 // EXECUTABLE STATEMENTS

 printf("\nEnter the number of bricks: ");
 scanf("%d", &bricks);
 printf("Enter the number of spheres: ");
 scanf("%d", &spheres);
 printf("Enter the number of prisms: ");
 scanf("%d", &prisms);
 printf("\nLongest possible string of alternating shapes usuing only two different shapes: %d", int final_string(int bricks, int spheres, int prisms));

 int final_string(int bricks,int spheres,int prisms);
{
 return bricks * (bricks > spheres & bricks > prisms) + spheres * (spheres > bricks & spheres > prisms) + prisms * (prisms > bricks & prisms > spheres);
}

return(0);
dtech
  • 47,916
  • 17
  • 112
  • 190

3 Answers3

4
  1. Remove all the ints from the line:

    printf("\nLongest possible string of alternating shapes usuing only two different shapes: %d", int final_string(int bricks, int spheres, int prisms));
    
  2. Remove the semicolon after the function signature in the definition of final_string

  3. Move final_string outside of main
  4. Use the logical && operator instead of the bitwise &
  5. Buy and read a C book
  6. In the future ask "questions" like this on http://codereview.stackexchange.com
Community
  • 1
  • 1
Taylor Brandstetter
  • 3,523
  • 15
  • 24
0

The line,

printf("\nLongest possible string of alternating shapes usuing only two different shapes: %d",  int final_string(int bricks, int spheres, int prisms));

is wrong. In this line, int final_string(int bricks, int spheres, int prisms) is declaration of function and not calling it. Replace it with final_string(bricks, spheres, prisms).

And define that function outside main. In C, function can't nest. So your corrected code would look,

#include<stdio.h>
#include<math.h>
int main(void)
{
    // LOCAL DECLARATIONS

   int bricks; //the number of bricks available
   int spheres; //the number of spheres available
   int prisms; //the number of prisms available
   int final_string(int bricks, int spheres, int prisms); //the longest possible string with the given shapes in an alternating fashion

   // EXECUTABLE STATEMENTS

   printf("\nEnter the number of bricks: ");
   scanf("%d", &bricks);
   printf("Enter the number of spheres: ");
   scanf("%d", &spheres);
   printf("Enter the number of prisms: ");
   scanf("%d", &prisms);
   printf("\nLongest possible string of alternating shapes usuing only two different   shapes: %d", final_string( bricks,  spheres, prisms));
   return(0);
}

int final_string(int bricks,int spheres,int prisms)
{
    return bricks * (bricks > spheres & bricks > prisms) + spheres * (spheres > bricks & spheres > prisms) + prisms * (prisms > bricks & prisms > spheres);
}
VoidPointer
  • 3,037
  • 21
  • 25
0

Here you go :

http://cfiddle.net/TyZAKw

As said above you put too many "int" ";" after the function signature when implementing it and & instead of &&

Avner Solomon
  • 1,486
  • 11
  • 17