• Support PF! Buy your school textbooks, materials and every day products Here!

Can't get this array to work-C

  • Thread starter sg001
  • Start date
  • #1
134
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 Im 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;
}
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,682
520
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.
 
  • #3
134
0
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;
}
 
  • #4
6,054
390
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.
 
  • #5
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,192
36
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 dont 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.
 
  • #6
33,505
5,191
Please wrap your code in [noparse]
Code:
[/noparse] tags
+1 on that
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 dont 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>
[color="blue"]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.
, 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.
 
  • #7
134
0
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.
 
  • #8
33,505
5,191
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?
... 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.
 
  • #9
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,192
36
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.

... 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
cepheid
Staff Emeritus
Science Advisor
Gold Member
5,192
36
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;[COLOR="Red"]i<num[/COLOR];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
134
0
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
Borek
Mentor
28,402
2,801
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
6,054
390
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
rcgldr
Homework Helper
8,682
520
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:

Related Threads on Can't get this array to work-C

Replies
2
Views
1K
  • Last Post
Replies
6
Views
2K
Replies
5
Views
2K
  • Last Post
Replies
2
Views
244
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
2
Views
1K
Replies
2
Views
727
Replies
3
Views
870
  • Last Post
Replies
12
Views
2K
  • Last Post
2
Replies
38
Views
3K
Top