Why Won't My C Program Compile for Finding the Smallest Integer in an Array?

  • Thread starter Thread starter sg001
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around a C programming problem where a user is attempting to write a program to find the smallest integer from a user-inputted array of up to ten integers. The focus is on debugging compilation errors and understanding array handling in C.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original code has compilation errors, including issues with the return type of main and syntax errors related to braces.
  • Some participants point out that the variable 'small' is not initialized, which could lead to undefined behavior.
  • There is confusion regarding the correct way to read multiple integers into an array using scanf.
  • Participants note that the loop condition 'i < num' is incorrect, as 'num' is an array and should not be used as a loop limit.
  • There are discussions about the proper declaration of the main function, with suggestions to avoid using 'void main' and to not include semicolons after #define statements.
  • One participant expresses a desire to define 'small' as the largest integer in the array, leading to further questions about array indexing and initialization.
  • Clarifications are provided on how to access elements of an array using indices, emphasizing that array indexing starts at 0.
  • There is a suggestion to initialize 'small' to the first element of the array to facilitate finding the smallest number.

Areas of Agreement / Disagreement

Participants generally agree on the need to correct the compilation errors and clarify array handling, but there is no consensus on the best approach to initialize 'small' or the overall structure of the program. Multiple competing views on coding practices and array usage remain.

Contextual Notes

Limitations include missing assumptions about user input handling, the need for proper initialization of variables, and the correct use of loop constructs in C. There are unresolved issues regarding the intended functionality of the program and the handling of user input.

Who May Find This Useful

This discussion may be useful for beginner programmers learning C, particularly those interested in array manipulation and debugging compilation errors in their code.

sg001
Messages
134
Reaction score
0

Homework Statement



I am writing a program so that a user can input up to ten integers and then sought through and print the smallest from that input

i.e. enter in up to ten integers: "7 5 6 19 4 8"
the smallest number is 4



Homework Equations





The Attempt at a Solution




Here's my attempt so far but I am getting compiling errors

min.c:5: warning: return type of ‘main’ is not ‘int’
min.c: In function ‘main’:
min.c:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
min.c:21: error: expected ‘{’ at end of input

but apart fro that I think my code is missing something but I don't know what

any help would be great.

code::

#include <stdio.h>
#define MAX 10
int main(int argc, char * argv[]){
int i,small;
int num[MAX];

printf("Enter up to %d integers:\n",MAX);
i=0;
scanf("%d", &num);

for(i=1;i<num;i++){
if(num<small){
small=num;
}
}

printf("The smallest element in the array is %d\n", small);
return 0;
}
 
Physics news on Phys.org
scanf("%d", &num);
That should be scanf("%d", &num); , and you'll need to initialize i. You'll need to figure some way to input an array of numbers.

if(num<small){
small is never initialized to any value.
 
ok is this getting there?

#include <stdio.h>
void main()
#define MAX 10;
int main(int argc, char * argv[]){
int i,small=;
int num[MAX];

printf("Enter up to %d integers:\n",MAX);
i=0;
scanf("%d", &num);

for(i=1;i<num;i++){
if(num<small){
small=num;
}
}

printf("The smallest element in the array is %d\n", small);
return 0;
}
 
1. That reads one integer, not ten.

2. The loop condition i < num is meaningless. i is an integer, and num is an array.

3. The initialization of small, small = is meaningless, too.
 
sg001 said:
ok is this getting there?

Please wrap your code in [noparse]
Code:
[/noparse] tags like so:

Code:
#include <stdio.h>
void main()
#define MAX 10;
int main(int argc, char *  argv[]){
   int i,small=[i];
   int num[MAX];

   printf("Enter up to %d integers:\n",MAX);
   i=0;
   scanf("%d", &num[i]);
   
   for(i=1;i<num;i++){
      if(num[i]<small){
      small=num[i];
      }
   }
   
   printf("The smallest element in the array is %d\n", small);
   return 0;
}

What is the purpose of the void main declaration at the top? I don't think you need this, especially when you have int main later on. Also, #define statements do NOT have semi-colons after them because they are compiler preprocessor directives, not lines of C code.
 
cepheid said:
Please wrap your code in [noparse]
Code:
[/noparse] tags
+1 on that
cepheid said:
like so:

Code:
#include <stdio.h>
void main()
#define MAX 10;
int main(int argc, char *  argv[]){
   int i,small=[i];
   int num[MAX];

   printf("Enter up to %d integers:\n",MAX);
   i=0;
   scanf("%d", &num[i]);
   
   for(i=1;i<num;i++){
      if(num[i]<small){
      small=num[i];
      }
   }
   
   printf("The smallest element in the array is %d\n", small);
   return 0;
}

What is the purpose of the void main declaration at the top? I don't think you need this
Not only that, but the OP shouldn't have that void main declaration, as it conflicts with the definition of main below it.

Also, a function declaration should end with a semicolon.

If you want to include the main declaration, the declaration should have the right return type, and the same parameter types as in the definition, something like this:
Code:
#include <stdio.h>
int[/color] main(int, char **);
#define MAX 10;
int main(int argc, char *  argv[])
{
  .
  .
  .

Note that there are several ways of declaring the 2nd parameter of main.
cepheid said:
, especially when you have int main later on. Also, #define statements do NOT have semi-colons after them because they are compiler preprocessor directives, not lines of C code.
 
Ok I want to somehow define small as the largest integer in the array.

Can I do it like this

Code:
 small=[MAX]

... I'm new to using arrays they seem a little hard to understand;;;

but how do I actually 'pick' and use a certain value from an array...

so if I had the array{1,3,5,6,2,9} and I want to pick the integer nine how would I do this?

Thanks.
 
sg001 said:
Ok I want to somehow define small as the largest integer in the array.

Can I do it like this

Code:
 small=[MAX]
No, for several reasons.
1. An assignment statement, like all statements in C, ends with a semicolon.
2. Brackets - [ ] - are used in array declarations and expressions involving arrays. I don't know what your purpose is in using brackets around MAX.
3. I don't know what you intend with MAX. Is it a variable or macro or what?
sg001 said:
... I'm new to using arrays they seem a little hard to understand;;;

but how do I actually 'pick' and use a certain value from an array...

so if I had the array{1,3,5,6,2,9} and I want to pick the integer nine how would I do this?
Iterating through the array with some kind of loop would be the usual way.

Is this for a class? Do you have a book you're working with? Every book on C programming has a section on arrays, with code examples.
 
sg001 said:
Ok I want to somehow define small as the largest integer in the array.

Can I do it like this

Code:
 small=[MAX]

1. You can't just put square brackets around a variable like that in C. It meaningless -- that is not how square brackets are used. Square brackets are used for indexing an array. Indexing an array means choosing one of its elements using an index that tells you which element you want. The index appears in square brackets after the name of the array variable. Note that array elements in C are numbered (or indexed) starting with 0, rather than starting with 1. For example, if you have an array called "num", then num[0] is the first element in that array. num[5] is the sixth element, and num is the (i+1)th element. But [5] just by itself is meaningless.

2. You say that you want to assign "small" the value of the largest integer in the array. But you don't know what the largest integer is beforehand, so I don't know how you would expect to be able to do this, short of writing a program to identify the largest element. In any case, why do you want to set small equal to the largest element in the array? Just initialize the value of small to ANY of the array elements. EDIT: for example, you could just say small = num[0] to start. You can choose whichever element in the array you want, why would it matter? The comparison in the for loop will still find array elements that are smaller.

sg001 said:
... I'm new to using arrays they seem a little hard to understand;;;

but how do I actually 'pick' and use a certain value from an array...

so if I had the array{1,3,5,6,2,9} and I want to pick the integer nine how would I do this?

Thanks.

I explained above how to index an array, which means to choose the element of that array whose position corresponds to that index. In the example you give above, if you define:

int a[6] = {1,3,5,6,2,9};

Then, in this example, these are the elements of the array:

a[0] is 1
a[1] is 3
a[2] is 5
a[3] is 6
a[4] is 2
a[5] is 9
 
Last edited:
  • #10
Oh, and while I'm at it:

Code:
#include <stdio.h>
void main()
#define MAX 10;
int main(int argc, char *  argv[]){
   int i,small=[i];
   int num[MAX];

   printf("Enter up to %d integers:\n",MAX);
   i=0;
   scanf("%d", &num[i]);
   
   for(i=1;i<num;i++){
      if(num[i]<small){
      small=num[i];
      }
   }
   
   printf("The smallest element in the array is %d\n", small);
   return 0;
}

As others have already pointed out, i < num makes no sense. To loop through all the elements in the array, you would need something like for(i = 0; i < MAX; i++). This is because MAX is the number of array elements, so the very last element will have an index that is one less than MAX.
 
  • #11
cepheid said:
1. You can't just put square brackets around a variable like that in C. It meaningless -- that is not how square brackets are used. Square brackets are used for indexing an array. Indexing an array means choosing one of its elements using an index that tells you which element you want. The index appears in square brackets after the name of the array variable. Note that array elements in C are numbered (or indexed) starting with 0, rather than starting with 1. For example, if you have an array called "num", then num[0] is the first element in that array. num[5] is the sixth element, and num is the (i+1)th element. But [5] just by itself is meaningless.

2. You say that you want to assign "small" the value of the largest integer in the array. But you don't know what the largest integer is beforehand, so I don't know how you would expect to be able to do this, short of writing a program to identify the largest element. In any case, why do you want to set small equal to the largest element in the array? Just initialize the value of small to ANY of the array elements. EDIT: for example, you could just say small = num[0] to start. You can choose whichever element in the array you want, why would it matter? The comparison in the for loop will still find array elements that are smaller.
I explained above how to index an array, which means to choose the element of that array whose position corresponds to that index. In the example you give above, if you define:

int a[6] = {1,3,5,6,2,9};

Then, in this example, these are the elements of the array:

a[0] is 1
a[1] is 3
a[2] is 5
a[3] is 6
a[4] is 2
a[5] is 9
But since I have declared MAX 10

then wouldn't small=[MAX]

be really small=[10] , the 10th element?

but how is this looking anyway

Code:
#include <stdio.h>
#define MAX 10
int main(int argc, char *  argv[]){
   int i,small=num[10];
   int num[MAX];

   printf("Enter up to %d integers:\n",MAX);
   i=0;
   scanf("%d", &num[i]);
   
   for(i=0;i<MAX;i++){
      if(num[i]<small){
      small=num[i];
      }
   }
   
   printf("The smallest element in the array is %d\n", small);
   return 0;
}

logically what I think my code should do now is start with the 0th element in the array sought through all elements in the array and compare the ith element to the current value of 'small'(the 10th element). the loop will finish when i=MAX.

does this seem ok now?
 
  • #12
I have a feeling you are ignoring everything that we write. You don't understand how tables work. You were already told more than once that small= is meaningless. is a way of addressing a table, so it requires a table to work - in your case num means ith element of the table num. num[MAX] would mean MAXth element of table num.

To make thing more convoluted tables in C start with 0th element, so ith element is in fact (i+1)th. But that will start to matter after you will understand how the tables work.
 
  • #13
At this point I would suggest getting a good book and reading it. What we are seeing here is what I call stochastic programming: doing things at random hoping they would just work without solid understanding of the matters involved.
 
  • #14
sg001 said:
I am writing a program so that a user can input up to ten integers and then sought through and print the smallest from that input

Here's a code fragment that inputs a string and then scans it for a list of numbers. I don't know if there's a more common method to input a string. See if you can complete the rest of the program now.

Code:
    int  i, small, size;
    int  num[MAX];
    char buffer[80];

    printf("Enter up to %d integers:\n", MAX);
    scanf(" %[^\n]", buffer);
    size = sscanf(buffer, "%d%d%d%d%d%d%d%d%d%d",
           &num[0], &num[1],
           &num[2], &num[3], &num[4], &num[5], 
           &num[6], &num[7], &num[8], &num[9]);
 
Last edited:

Similar threads

  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
9
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 3 ·
Replies
3
Views
2K