Help me correct this code please

+2 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 comma
Calculate (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 arr
Display the first prime integer of arr




This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
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;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Mathias Frits Rørvik · November 11, 2014
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


int main(void)
{
    int n;
    printf("Trailing character!\n");
    while(1) //there is no such thing as true, everything other than 0 is true
    {
        printf("Enter n: ");
        scanf("%d", &n);
        if(n <= 3)
        {
            printf("Out of range!\n");
            break;
        }
    }

    int arr[30]; //CHANGED TO INT
    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]); //You are dealing with int's not chars..
        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;
}



The code now compiles, you had used true, which is not a keyword in C. Also you were using scanf to read INTS to a CHAR array.
0 Teemmy Ellorry · November 20, 2014
Thanks very much, but the "First prime number isn't working yet"...
0 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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
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; i<m; i++)
    {
        printf("Element %d : ",i+1);
        scanf("%d",&arr);
    }
    printf("All elements: ");
    for(i=0; i<m; i++)
    {
        printf("%d ,",arr);
    }
    printf("\n");
    a=arr[1];
    b=arr[m-1];
    sum=((a*a*a) + (b*b*b));
    
       printf("sum is: %d: \n",sum);
    
    max=arr[0];
    for(i=0; i < m; i++)
    {
        for(j=1;j<m;j++)
        {
            if(arr>arr[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.
0 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 <stdio.h>
#include <stdlib.h>
#include <ctype.h>
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;
       }
    }
}
0 Teemmy Ellorry · November 20, 2014
I used "fflush" to clear the buffered memory temporarily
-1 Teemmy Ellorry · November 21, 2014
Standard input stream
The 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
0 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.
  • 1

C

107,040 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator