the problem with this is that after it accepts the four (4) integers it stop and dont go on to find the average. IS THERE ANYONE OUT THERE THAT CAN HELP. NUFF RESPECT TO ALL WHO TRY TO HELP.

//to calculate the average of 4 numbers
#include<stdio.h>
int get4ints(){//function to collect 4 integers
int count,*num,number[4];
for(count=0;count<4;count++){
if (count==0){printf("you are required to enter 4 integers\n");
}
printf("enter number\n");
scanf("%d",&number[count]);}
return *number;}//return the value of the first element in the array
//main program to call the array for 4 ints and return average
int main()
{
int *num,number,sum=0,count,exit; 
*num=get4ints();//num points to the first element of the array
for(count=0;count<4;count++){//loop to print the values in the array
*num=*(num+count);
printf("number is %d",*num);
sum=sum+*num;}
int average=sum/4;//calculate the average
printf("the average is %d",average);//print the average
printf("enter a key to exit");//A KEY IS ENTERED THEN THE PROGRAM EXIT
scanf("%d",&exit);
}
Salem commented: This weeks star prize for the most useless thread title - congratulations. -1
Member Avatar for GreenDay2001

How about putting all of them in a function::

#include<stdio.h>

int get4ints()
{//function to collect 4 integers
    int count,num=0,number[4];
    for(count=0;count<4;count++)
    {
        if (count==0) printf("you are required to enter 4 integers\n");
        printf("enter number\n");
        scanf("%d",&number[count]);
        num+=number[count];
    }
    num/=4;
    return num;
}//return the value of the first element in the array
    //main program to call the array for 4 ints and return average
int main()
{
    int average=get4ints();//calculate the average
    printf("the average is %d",average);//print the average
    getchar();
}
//to calculate the average of 4 numbers
int get4ints()
{
    //function to collect 4 integers
    int count,*num,number[4];
    for(count=0;count<4;count++)
    {
        if (count==0)
        {
            printf("you are required to enter 4 integers\n");
        }
        printf("enter number\n");
        scanf("%d",&number[count]);
    }
    //return the value of the first element in the array
    return *number;
}

//main program to call the array for 4 ints and return average
int main()
{
    int *num=new int(),number,sum=0,count,exit;
    *num=get4ints();//num points to the first element of the array
    for(count=0;count<4;count++)
    {//loop to print the values in the array
        *num=*(num+count);
        printf("number is %d",*num);
        sum=sum+*num;
    }
    int average=sum/4;//calculate the average
    printf("the average is %d",average);//print the average
    printf("enter a key to exit");//A KEY IS ENTERED THEN THE PROGRAM EXIT
    scanf("%d",&exit);
    return 0 ;
}

It doesn't work coz there are lotsa problems:
1.You're returning the value of first element of the array (line 16). Later you're using it as if it the address of first element (line 26). Seems like a confusion between "pointer to an integer" and "integer"...
2. number[4] is a local array, you're returning address of it's first element, the array goes out of scope when func returns so the address you've returned becomes invalid.
3. int* num was not initialized and you were dereferencing it (line 23) => ka-boom !

What you need to do is:
Create a array in the function dynamically (malloc or new).
Return the "address" of first element (instead of value).
Rest will more or less remain same.

Next time use code tags instead of manually coloring the code. Never do a machine's job.. :)

How about putting all of them in a function::

#include<stdio.h>

int get4ints()
{//function to collect 4 integers
    int count,num=0,number[4];
    for(count=0;count<4;count++)
    {
        if (count==0) printf("you are required to enter 4 integers\n");
        printf("enter number\n");
        scanf("%d",&number[count]);
        num+=number[count];
    }
    num/=4;
    return num;
}//return the value of the first element in the array
    //main program to call the array for 4 ints and return average
int main()
{
    int average=get4ints();//calculate the average
    printf("the average is %d",average);//print the average
    getchar();
}

At least change the name of "get4ints()".. it doesn't get 4 ints anymore...

//to calculate the average of 4 numbers
int get4ints()
{
    //function to collect 4 integers
    int count,*num,number[4];
    for(count=0;count<4;count++)
    {
        if (count==0)
        {
            printf("you are required to enter 4 integers\n");
        }
        printf("enter number\n");
        scanf("%d",&number[count]);
    }
    //return the value of the first element in the array
    return *number;
}

//main program to call the array for 4 ints and return average
int main()
{
    int *num=new int(),number,sum=0,count,exit;
    *num=get4ints();//num points to the first element of the array
    for(count=0;count<4;count++)
    {//loop to print the values in the array
        *num=*(num+count);
        printf("number is %d",*num);
        sum=sum+*num;
    }
    int average=sum/4;//calculate the average
    printf("the average is %d",average);//print the average
    printf("enter a key to exit");//A KEY IS ENTERED THEN THE PROGRAM EXIT
    scanf("%d",&exit);
    return 0 ;
}

It doesn't work coz there are lotsa problems:
1.You're returning the value of first element of the array (line 16). Later you're using it as if it the address of first element (line 26). Seems like a confusion between "pointer to an integer" and "integer"...
2. number[4] is a local array, you're returning address of it's first element, the array goes out of scope when func returns so the address you've returned becomes invalid.
3. int* num was not initialized and you were dereferencing it (line 23) => ka-boom !

What you need to do is:
Create a array in the function dynamically (malloc or new).
Return the "address" of first element (instead of value).
Rest will more or less remain same.

Next time use code tags instead of manually coloring the code. Never do a machine's job.. :)

Insted of putting " * //function to collect 4 integers" you put "//function to collect 4 integers"

it works perfectly but can you show how to return the array name(which is the pointer to the first element in the array) then print and find the average in the main programme.

it works perfectly but can you show how to return the array name(which is the pointer to the first element in the array) then print and find the average in the main programme.

No, it's your job to show us how you think it's done, and we will help you correct it if it doesn't work. Please read this, this, and this to help you understand how we can help you best.

can anyone figure out why this program is printing different numbers from what is input and also calculating the wrong average. answer ASAP PLEASE.


/*program to collect 4 integer in the module get4ints
and then return the values to the main program where the
numbers are printed and average calculated*/

#include<stdio.h>
int get4ints()
{
//function to collect 4 integers
int count,*num,number[4];
for(count=0;count<4;count++)
{
if (count==0)
{
printf("you are required to enter 4 integers\n");
}
printf("enter number\n");
scanf("%d",&number[count]);
}
//return the value of the first element in the array
return *number;
}
//main program to call the array for 4 ints and return average
int main()
{
int *num=new int(),number,sum=0,count,exit;
*num=get4ints();//num points to the first element of the array
for(count=0;count<4;count++)
{//loop to print the values in the array
*num=*(num+count);
printf("\nnumber is %d",*num);
sum=sum+*num;
}
int average=sum/4;//calculate the average
printf("\nthe average is %d",average);//print the average
printf("\nenter a key to exit");//A KEY IS ENTERED THEN THE PROGRAM EXIT
scanf("%d",&exit);
return 0 ;
}

> *num=get4ints();
Your program doesn't make any sense. You're trying to use C++ allocation (new) in a C program, trying to force an entire array into a single integer, and overall getting pointers and the actual memory mixed up.

Perhaps you should review your pointer knowledge here...
http://www.cprogramming.com/tutorial/c/lesson6.html

can anyone figure out why this program is printing different numbers from what is input and also calculating the wrong average. answer ASAP PLEASE.

I hate being ignored!!! READ THIS!

commented: Just delete the rubbish until they learn some netiquette! - Salem +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.