0

Hi guys I am new to this forum so I am seeking to improve my programming language skill starting with C. I have subscribed to this forum to support me as a guide.

I am having a small problem printing this "Maximum Value in array" ,using gcc in Ubuntu please let me know where I can correct my mistakes and I do apologize for my English it is not my native language.

#include<stdio.h>

int max(int a[10] ,int );
int main()
{
    int a[100];
    int i;
    int j;

    printf("enter how many  numbers that too be printed\n");
    scanf("%d",&j);

    for(i=0;i<=j-1;i++) {
        scanf("%d",&a[i]);
    }
    printf("the original is \n");
    for(i=0;i<=j-1;i++)
    {
        printf("%4d",a[i]);
    }
    printf("\n");

    printf("the maximum value is %d\n",max(a,j));
    return 0;
}

int max(int a[10] ,int b)
{ 
    int mx=0;
    int k;
    int c=0;              
    for(k=1;k<=b;k++)
    { 
        if((a[k]>a[c]) && (a[k]>mx ))
            mx=a[k];
    }
}
}
return (mx);
}

Compile error shows ')' missing this brackets at the end of line even though I have corrected it and even if I Ignore the results are completely different.

4
  • "... I do apologize for my English it is not my native language". No need to apologize. You are about to improve your skills in two languages for the price of one. That being said, please fix your indentation. Commented Aug 28, 2014 at 17:35
  • Your code is very badly indented; please try to indent it properly and you will see the problems. Commented Aug 28, 2014 at 17:40
  • Good IDE makes programmer's life so much easier :)) Commented Aug 28, 2014 at 17:42
  • Thank you Mad P for the suggestion Commented Aug 28, 2014 at 18:43

2 Answers 2

2

You got 1 missing } in your code. Here's the corrected code.

#include <stdio.h>

int max(int a[10], int);
int main() {
    int a[100];
    int i;
    int j;

    printf("enter how many  numbers that too be printed\n");
    scanf("%d", &j);

    for (i = 0; i <= j - 1; i++) {
        scanf("%d", &a[i]);
    }
    printf("the original is \n");
    for (i = 0; i <= j - 1; i++) {
        printf("%4d", a[i]);
    }
    printf("\n");

    printf("the maximum value is %d\n", max(a, j));
    return 0;
}

int max(int a[10], int b) {
    int mx = 0;
    int k;
    int c = 0;
    for (k = 1; k <= b; k++) {
        if ((a[k] > a[c]) && (a[k] > mx)) {
            mx = a[k];
        }
    }

    return (mx);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you .. I will be using eclipse as you have hinted
k <= b should be k < b; as-is this code reads one more entry than was input . (Also it should check j <= 100)
1

As you can see in the question, Johnny Mopp has fixed the indentation of your code.

By doing that, it's pretty easy to see that at the end of your code, you have two extra closing braces, that is, unmatched } characters.

You'll want to figure out the correct location of those brace characters.

Also, note that it's an extra curly brace, not an extra parenthesis.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.