user3469926
user3469926

Reputation: 3

what is wrong with my code in c

I have problem in my C code. The function I want to write is an array that take 100 numbers and print the prime numbers that are less than the average of the numbers in my array. I have an error and I don't know why my code is not working:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE 100

void Array123( int arr[] ) {

    int i = 0, arr[SIZE], j, count = 0;
    float sum = 0, avg;

    do{
        scanf( " %d", &arr[i] );
        i++;
        count++;
        sum += arr[i];
    }
    while ( ( arr[i] != 0 ) || ( i < SIZE ) );

    avg = sum / count;

    for ( j = 2; j <= arr[i] - 1; j++ ) {
        if ( arr[i] % j == 0 ) {
            break;
        }
    }

    if ( j == arr[i] ) {
        if ( arr[i] < avg ) {
            printf( "%d", arr[i] );
        }
    }
}

int main( ) {
    int arr[SIZE], i, x;
    for ( i = 0; i < SIZE; i++ ) {
        x = Array123( arr[SIZE] );
        printf( " %d", x );
    }
}

Note that I'm allowed to use arrays without pointers.

The errors are:

Warning 2   warning C4047: 'function' : 'int *' differs in levels of indirection from 'int' 
Warning 3   warning C4024: 'Array123' : different types for formal and actual parameter 1   
        5   IntelliSense: argument of type "int" is incompatible with parameter of type "int *"     41  14  
Error   4   error C2120: 'void' illegal with all types      41  1   
Error   1   error C2082: redefinition of formal parameter 'arr' 

Upvotes: 0

Views: 1045

Answers (3)

user3469926
user3469926

Reputation: 3

thank you ,i have no errors now but i realized my code wasn't good enough so i changed it to become more reasonable.

   #define _CRT_SECURE_NO_WARNINGS 
   #include <stdio.h>
   #define size 100

    void PrimeNumbers(int size)
    {
int num,i,sum=0,count=0,arr[size],flag=0,j;
float avg;
do{

    scanf(" %d",&num);
    if(num >0)
    {
        arr[i]=num;
    i++;
    count++;
    sum+=num;
    }while (i<100 && num!=0 )
        avg=sum/count;
    for (i=0;i<count;i++)
    {
        for ( j = 2; i < arr[i]; i++)
        {
    if (arr[i] % j == 0)
    flag=1;
    if (flag =1 && arr[i]<avg)
        printf("the prime number that less than average\n %d ",arr[i]);
             }

Upvotes: 0

David Ranieri
David Ranieri

Reputation: 41017

The main problem is that you are redeclaring arr in your function

Upvotes: 1

selbie
selbie

Reputation: 104474

You are redeclaring "arr" as a local variable which overrides the the function parameter:

Change this line:

int i=0,arr[SIZE],j,count=0;

To this:

int i=0,j,count=0;

Upvotes: 0

Related Questions