# Help me correct this code please

 Teemmy Ellorry · November 5, 2014 Enter an integer n > 3 (check input validation).Enter n integers that are elements of an array arr, each line for each element. (does not check input validation)Display all elements of array, separated by commaCalculate (a­3 + b3), a is the second element and b is the last element of array arr  (1 mark)Find the maximum element of array arrDisplay the first prime integer of arr This is my code:#include #include #include #include int main(){    int n;    printf("Trailing character!\n");    while(true)    {        printf("Enter n: ");        scanf("%d", &n);        if(n <= 3)        {            printf("Out of range!\n");            break;        }    }        char arr[30];    int i = 1, j, sum = 0, max, b;    printf("Enter n: ");    scanf("%d", &j);    printf("\n\n");    for(int k = 0; k < j; k++)    {        printf("Enter elements %d: ", i);       scanf("%d", &arr[k]);       i++;       max = arr[k];       if(max > arr[k])       {           max = arr[k];       }    }       for(int k = 1; k <= arr[k] - 1; k++)       {           if(arr[k] % k != 0 && k == arr[k] - 1)           {               b = arr[k];           }       }    printf("\n");    printf("All elements: ");    for(int k = 0; k < j; k++)    {        printf("%d,", arr[k]);    }    sum = pow(arr[1], 3) + pow(arr[4], 3);    printf("\nsum: %d\n", sum);    printf("Maximum element: %d\n", max);    printf("First prime integer: %d", b);    return 0;}

## Replies

 Teemmy Ellorry · November 20, 2014 Thanks very much, but the "First prime number isn't working yet"... Teemmy Ellorry · November 20, 2014 ``/*    (scanf("%d%c", &rows, &c)!=2 || c!='\n')In the example1: this condition is TRUE because scanf return 0 (!=2)In the example2: this condition is FALSE because scanf return 2 and c == '\n'In the example3: this condition is TRUE because scanf return 2 and c == 'a' (!='\n')*/#include #include #include #include int main(){    int n,m,a,b,i,count,j;    char c;    j=0;    int arr[100];    int sum,max,prime,last;    prime=sum=max=last=a=b=0;        do    {        printf("Enter n: ");        n=scanf("%d%c",&m,&c);                if(n==2 && c!='\n')        {            printf("Trailing characters!\n");            fflush(stdin);        }        else if(!isdigit(m) && c!='\n')        {            printf("Incorrect input\n");            fflush(stdin);        }        else if (m<3)        {            printf("Out of range!\n");            fflush(stdin);            fflush(stdin);        }        else if(c=='\n')        {            j=1;        }        }while(j==0);        for(i=0; iarr[j] )            {                max=arr[j];            }            else            {                max=arr;            }        }            }        printf("Maximum element: %d\n",max);        for (i = 0; i <= n; i++)    {        count=0;         for (j = 1; j <= arr; j++)              {                  if(arr%j==0)                {                    count++;                }                                          if (count==2  && arr==j )                {                prime=arr;                goto out;                }            }        }    out:        printf("First prime integer: %d\n\n",prime);}``I did it this way and it displayed the 1st prime in the array. Teemmy Ellorry · November 20, 2014 Design and code a function named getDouble that receives two double values - a lower limit and an upper limit - and returns user input that lies between the limiting values.  Your function rejects any input that includes trailing characters or lies outside the specified limits.  This means accepting Upper and lower case alphabets ONLY."My code is treating digits as characters i don't know why please HELP"``#include #include #include char getDouble(char a[100]);int main(){    char a[100];    printf("Enter Password: ");    gets(a);    getDouble(a);    return 0;}char getDouble(char a[100]){    for(int k = 0; k != '\n'; k++)    {        if(!isdigit(a[k]) && isalpha(a[k]))       {           printf("Accepted");           break;       }       else       {           printf("Trailing character");           break;       }    }}`` Teemmy Ellorry · November 20, 2014 I used "fflush" to clear the buffered memory temporarily Teemmy Ellorry · November 21, 2014 Maybe you should enlighten me better... am still a novice in C, at least that is what i was taught, which i just explained to you. Teemmy Ellorry · November 21, 2014 Standard input streamThe standard input stream is the default source of data for applications. In most systems, it is usually directed by default to the keyboard. stdin can be used as an argument for any function that expects an input stream (FILE*) as one of its parameters, like fgets or fscanf. Although it is commonly assumed that the source of data for stdin is going to be a keyboard, this may not be the case even in regular console systems, since stdin can generally be redirected on most operating systems at the time of invoking the application. For example, many systems, among them DOS/Windows and most UNIX shells, support the following command syntax: myapplication < example.txt to use the content of the file example.txt as the primary source of data for myapplication instead of the console keyboard. It is also possible to redirect stdin to some other source of data from within a program by using the freopen function. If stdin is known to not refer to an interactive device, the stream is fully buffered. Otherwise, it is library-dependent whether the stream is line buffered or not buffered by default
• 1

## C

126,321 followers