Can't get this array to work-C

  • Thread starter sg001
  • Start date
  • Tags
    Array
In summary: So the first element is element 0, the second element is element 1, and so on.2. It is not clear what you are trying to do with the variable small. If you want it to hold the largest integer in the array, then you are going to have to write some code that finds the largest integer in the array and puts it into small. You don't just write [MAX] and expect the compiler to figure out what you want to do.3. The variable MAX is not a good name for that variable. By convention, names that are all capital letters are used for macros, not for variables. But your variable is not a macro, it is a
  • #1
sg001
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 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
  • #2
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
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
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
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.
 
  • #6
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>
[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.
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.
 
  • #7
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
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.
 
  • #9
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;[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
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:

1. Why is my code not working even though I have declared the array correctly?

There could be a few reasons for this. First, make sure you are accessing the correct index of the array. Remember that array indices start at 0, so if you are trying to access the 5th element, you should use index 4. Additionally, double check that you have correctly initialized the array with values. Lastly, ensure that you are using the correct data type for the elements in the array.

2. How can I add or remove elements from an array in C?

In C, arrays have a fixed size and cannot be resized. However, you can create a new array with a larger or smaller size and use functions such as memcpy() to copy elements from one array to another. Alternatively, you can use dynamic memory allocation functions, such as malloc() and free(), to allocate memory for an array at runtime.

3. Can I have an array with multiple data types in C?

No, arrays in C are homogeneous, meaning they can only store elements of the same data type. If you need to store multiple data types in a single data structure, you can use a struct or a union.

4. How can I loop through an array in C?

You can use a for loop to iterate through each element of the array. The loop should start at index 0 and end at the last index (size - 1). Alternatively, you can use a while or do-while loop with a counter variable to traverse the array.

5. What is the difference between an array and a pointer in C?

In C, arrays and pointers are closely related, and in many cases, an array name can be used as a pointer to the first element in the array. However, there are some differences. For example, the size of an array is fixed and cannot be changed, whereas a pointer can be reassigned to point to a different memory location. Additionally, pointer arithmetic is possible, but not with arrays.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
872
  • Engineering and Comp Sci Homework Help
Replies
3
Views
743
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
661
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top