help with c program

tmh88tmh88 Pittsburgh / Athens, OH
edited February 2007 in Internet & Media
hey, i cant get this program to work, any advice. I need to use arrays for this. The user inputs 20 integers, and the program displays the largest integer, and the subscript of it in the array (its placement in the array). When I compile it, it says.
In function 'int main()':
lab8.c:22: error: expected ',' or '...' before numeric constant
lab8.c:22: error: a function-definition is not allowed here before '{' token
lab8.c:29: error: expected `}' at end of input

If anyone see something wrong please let me know.


The program is below this, when I put in [code] ["slash" code] it only displays like 1/4 of the program. Sorry, but I'm just gonna post it wihtout that because thats the only way it will show.


#include<stdio.h>
#define n 20
int maxAndIndex(int*, int);
int main(){
int i,a;

for(i=0;i<n;i++){
printf("Enter an integer");
scanf("%d",&a);
maxAndIndex(a,n);
printf("%d",a);

}
return(0);
}

int maxAndIndex(int a[], int n); {
int i, cur_large;
cur_large = a[0];
for(i=1;i<n;i++)
if (a > cur_large)
cur_large=a;
return (cur_large);
}

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2007
    1) You have mismatched #s of curly braces. What are you using to edit this? Even emacs will manage indents/braces and help you see this.
    2) I don't think that "int i, a;" works. Because i is always zero, won't a just be a pointer (even if it works at all)?
    3) Your for loops are all kinds of messed up. Missing a parenthesis, an exit statement, and you're not incrementing your loop variable (you'll probably be in it forever).

    You need to write small amounts of code, compile it to see if it works, change a little, compile it...etc.
  • a2jfreaka2jfreak Houston, TX Member
    edited February 2007
    i is not zero. i is whatever value is at that memory location.

    The most clear and readable way to do what he wants (if he really wants a to be an array/pointer) is:
    [php]
    int i;
    int* a;
    [/php]
    shwaip wrote:
    1) You have mismatched #s of curly braces. What are you using to edit this? Even emacs will manage indents/braces and help you see this.
    2) I don't think that "int i, a;" works. Because i is always zero, won't a just be a pointer (even if it works at all)?
  • tmh88tmh88 Pittsburgh / Athens, OH
    edited February 2007
    Ok I changed it to this and it compiles now....the only problem is that once I get to the 10th integer I get a core dump error.


    this is the output
    "Enter an integer1
    1Enter an integer2
    2Enter an integer3
    3Enter an integer4
    4Enter an integer5
    5Enter an integer6
    6Enter an integer7
    7Enter an integer8
    8Enter an integer9
    9Enter an integer10
    Segmentation Fault (core dumped)
    "

    #include<stdio.h>

    int maxAndIndex(int*, int);
    int main(){
    int i,a,n;
    n=20;

    for(i=0;i<n;i++){
    printf("Enter an integer");
    scanf("%d",&a);
    maxAndIndex(a,n);
    printf("%d",a);

    }
    return(0);
    }

    int maxAndIndex(int a[80], int n) {
    int i, cur_large;
    cur_large = a[0];
    for(i=1;i<n;i++)
    if (a > cur_large)
    cur_large=a;
    return (cur_large);
    }
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2007
    2) I don't think that "int i, a;" works.

    (ps. it doesn't what you think, whatever that is).

    1) not having your code in either or tags is f'ing up your code something hardcore. It's hard to read because of the lack of indents and (after a quick look at the page source), it's taking out parts that are pretty important. Either use the tags, or attach a file.
    2a) The reason you're getting core dumps is because you're accessing memory you aren't supposed to.

    sorry i missed your im earlier, i had just gone to sleep.
  • a2jfreaka2jfreak Houston, TX Member
    edited February 2007
    This should be what his code looks like.
    [php]
    #include<stdio.h>

    int maxAndIndex(int*, int);

    int main() {
    int i,a,n;
    n=20;

    for(i = 0; i < n; i++){
    printf("Enter an integer");
    scanf("%d", &a);
    maxAndIndex(a, n);
    printf("%d", a);
    }

    return(0);
    }

    int maxAndIndex(int a[80], int n) {
    int i, cur_large;
    cur_large = a[0];
    for(i = 1; i < n; i++)
    if (a > cur_large)
    cur_large = a;

    return (cur_large);
    }
    [/php]
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2007
    some questions that should help you figure out the problems, because if I fix it, then I should get the credit :P.

    1) How long is your 'a' array?
    2) Why do you have a[80] in the maxAndIndex, rather than a pointer?
    3) What are you doing with the return value from maxAndIndex()?
Sign In or Register to comment.