need help :(

Pages: 12
#include <stdio.h>

int main(void)
{
int numbers[80], array[1];
int count = 0,J;
long sum = 0;
float average = 0.0;

do
{
printf( "Please Enter How Many Number You Want to Add(1~80)" );
scanf( "%d", &J );
} while( ( J < 1 ) || ( J > 80 ) );

{
// Invalid input. 'numbers' can only hold 80.
}
printf("Please input the biggest number");
scanf("%d",&array[1]);

int i = 0;
for( ; i < J-1; i++ )
{
printf("%2d",i);
scanf("%d", &numbers[i]);
sum =array[1]-numbers[i];
printf("%d",sum);
}


printf("\nSum of the numbers entered is: %f\n", numbers[i]);
return 0;
}
This Subtration oso have some problems :(
closed account (zb0S216C)
Does this work for you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    // Numbers: ...... Contains the allocated resources.
    // BiggestNum: ... Holds the biggest number within 'Numbers'.
    // InputBuffer: .. Contains the value given by the user.
    // Total: ........ Contains the total of all numbers within 'Numbers'.
    // I: ............ Used within the 'for' loop as the counter.
    // Average: ...... Holds the average of 'Total'.
    int *Numbers = NULL, BiggestNum = 0;
    int InputBuffer = 0;
    long Total = 0;
    int I = 0;
    float Average = 0.0f;

    // Request the user input.
    do
    {
        // Inform the user to input a value (make sure they know what the value
        // they're entering is for). If the given value is less than or equal
        // to zero, or, greater than 80, make then enter another number.
        // Otherwise, break from the loop.
        printf( "Please Enter How Many Number You Want to Add(1~80): " );
        scanf( "%d", &InputBuffer );
    } while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );

    // Dynamically create the array based on the user input (InputBuffer).
    Numbers = ( ( int * )calloc( InputBuffer, sizeof( int ) ) );

    if( Numbers == NULL )
    {
        // The allocation of 'Numbers' failed. Inform the user that the program
        // will end.
        printf( "Failed to allocate the array. Exiting" );
        getchar( );
        return 1;
    }

    // Here, we'll enter a loop which will perform 'n' passes. The amount of
    // passes is determined by the value within 'InputBuffer'.
    for( I = 0; I < InputBuffer; I++ )
    {
        // We'll use 'BiggestNum' here for two reasons:
        // 1) To save stack space.
        // 2) 'BiggestNum' isn't used until later on.
        do
        {
            printf( "Enter number[%d]: ", ( I + 1 ) );
            scanf( "%d", &BiggestNum );
        } while( BiggestNum < 0 );
        Numbers[I] = BiggestNum;
    }

    // Clear 'BiggestNum' from the last operation.
    BiggestNum = 0;

    // Here, we'll find the biggest number and store it within 'BiggestNum'.
    for( I = 0; I < InputBuffer; I++ )
        if( BiggestNum < Numbers[I] )
            BiggestNum = Numbers[I];

    // We'll enter another loop here. This time, we'll add up all if the numbers
    // within 'Numbers'. The result of the computation will be stored within the
    // previously defined variable, 'Total'.
    for( I = 0; I < InputBuffer; I++ )
        Total += Numbers[I];

    // Compute the average.
    Average = ( Total / 2.0f );

    // Release the allocated memory. If we don't, we'll have leaking memory.
    free( Numbers );

    // Print the value stored within 'Total'.
    printf( "\nSum of the numbers entered is: %d\n", Total );

    // Print the average of 'Total'.
    printf( "Average: %f\n", Average );
    scanf( "%*d", NULL );
    return 0;
}


Note that scanf( ) is unstable. If you're using a Microsoft specific compiler, consider using scanf_s( ).

Wazzak
Last edited on
closed account (DSLq5Di1)
Why on earth are you using an integer array with one element to store a number.. ? Your error comes from accessing array[1] // 2nd element when only array[0] // 1st element exists.
hm.. so if i want to do a multiplication all i have to do is change the + from the total to *??
n i mean with he total also
how about subtraction i am having difficult still i am using 2 array - array1 & array2 but they does not seem to be subtract >.<
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float *Numbers = NULL,J[1], BiggestNum = 0,Total = 0,Q;
int InputBuffer = 0;
int I = 0 , Total1=0;
do
{
printf( "Please Enter How Many Number You Want to Add(1~80): " );
scanf( "%d", &InputBuffer );
} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );
Numbers = ( ( float * )calloc( InputBuffer, sizeof( int ) ) );
if( Numbers == NULL )
{
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}

{
printf("Enter Number[1]: ");
scanf("%f",&Q);
J[2] = Q ;
}
{
for( I = 0; I < InputBuffer; I++ )
{

printf( "Enter number[%d]: ", ( I + 2 ) );
scanf( "%f", &BiggestNum );
Numbers[I] = BiggestNum;
}

BiggestNum = 0;

for( I = 0; I < InputBuffer ; I++ )
if( BiggestNum < Numbers[I] )
BiggestNum = Numbers[I];

for( I = 0; I < InputBuffer; I++ )
Total = J[1] -= Numbers[I];


printf( "\nSum of the numbers entered is: %f\n", Total );
return 0;
}
}
[This Is My Subtraction , It Have Some RunTime Error >.<||]
bumpsssssss
Your problem is the array J[1] (whiche is an array of 1 element). Later you're using J[2] (3. element) and J[1] (2. element) which is of course out of bounds.

EDIT: you seem to be resistant against [code]Your code[/code]?
Last edited on
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float *Numbers = NULL,J[1], BiggestNum = 0,Total = 0,Q;
int InputBuffer = 0;
int I = 0 , Total1=0;
do
{
printf( "Please Enter How Many Number You Want to Add(1~80): " );
scanf( "%d", &InputBuffer );
} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );
Numbers = ( ( float * )calloc( InputBuffer, sizeof( int ) ) );
if( Numbers == NULL )
{
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}

{
printf("Enter Number[1]: ");
scanf("%f",&Q);
J[1] = Q ;
}
{
for( I = 0; I < InputBuffer; I++ )
{

printf( "Enter number[%d]: ", ( I + 2 ) );
scanf( "%f", &BiggestNum );
Numbers[I] = BiggestNum;
}

BiggestNum = 0;

for( I = 0; I < InputBuffer ; I++ )
if( BiggestNum < Numbers[I] )
BiggestNum = Numbers[I];

for( I = 0; I < InputBuffer; I++ )
Total = J[1] -= Numbers[I];


printf( "\nSum of the numbers entered is: %f\n", Total );
return 0;
}
}
[ i change it to J[1] & it still hav some problems [ code seem messy when trying to get osmething done >.<]]
Use code tags: [code]Your code[/code]

You don't understand arrays. If you say float J[1] you have exactly 1 element. The index of an array starts with 0. So
1
2
3
float J[1];
J[0] = 0.0; // OK
J[1] = 0.1; // Error! 
1
2
3
4
float J[2];
J[0] = 0.0; // OK
J[1] = 0.1; // OK
J[2] = 0.2; // Error! 
Within loop you say e.g.
1
2
3
4
5
float J[3];
for(int i = 0; i < 3; ++i)
{
  J[i] = i;
}
Last edited on
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
float *Numbers = NULL,J[80], BiggestNum = 0,Total = 0, Q;
int InputBuffer = 0;
int I = 0 , Total1=0;
do
{
printf( "Please Enter How Many Number You Want to subtract(1~80): " );
scanf( "%d", &InputBuffer );
InputBuffer = InputBuffer-1;
} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );
Numbers = ( ( float * )calloc( InputBuffer, sizeof( int ) ) );
if( Numbers == NULL )
{
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}

{
printf("Enter Number[1]: ");
scanf("%f",&Q);
J[80] = Q ;
}
{
for( I = 0; I < InputBuffer; I++ )
{

printf( "Enter number[%d]: ", ( I + 2 ) );
scanf( "%f", &BiggestNum );
Numbers[I] = BiggestNum;
}

BiggestNum = 0;

for( I = 0; I < InputBuffer ; I++ )
if( BiggestNum < Numbers[I] )
BiggestNum = Numbers[I];

for( I = 0; I < InputBuffer; I++ )
Total = J[80] -= Numbers[I];


printf( "\nSubtract of the numbers entered is: %f\n", Total );
return 0;
}
}
[ There Is Still Poblems i Change It to 80 but i can run the program few hours ago now i does not seem to be able to run it>.<]
Use code tags: [code]Your code[/code]

But you still have the same problem.

What's this line
J[80] = Q ;
good for? Why do you think that you have to store 'Q' at the 81. place 0 is the first index!(which is out of bounds since your array goes from 0...79) of your 'J' array? Why not using Q directly?

What's your intention in case of 'J' anyway?
You define a variable 'BiggestNum' but you don't use it?

What are you trying to do here?
1
2
for( I = 0; I < InputBuffer; I++ )
Total = J[80] -= Numbers[I];
It subtracts each number from the first hence the last subtraction is what you'll see
i was looking the output that look something like this.
How many numbers do you want to subtract: 3
Please enter number 1: 12
Please enter number 2: 2.5
Please enter number 3: 0.25
12 - 2.5 - 0.25 = 9.25

The purpose for J[80]=Q;
is so that the user who enter Q will be convert to J[80] for better understanding [for me ]
since your array goes from 0...79) of your 'J' array? Why not using Q directly?
i hope to do an array format

Q will be convert to J[80]
no conversion. The value of Q will be copied to J at the specified position (80 which is out of bounds)

I simplified the whole program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  float Numbers[80], CalcNum = 0, BiggestNum = 0; // This is needed to evaluate the numbers
  int NumCount = 0; // This is the amount of numbers
  int I = 0; // This is used as an index of the numbers
  do // Asking the user how many numbers he want to enter (limit it to 80 = the array size)
  {
    printf( "Please Enter How Many Number You Want to subtract(1~80): " );
    // The following scanf expects a pointer to int
    scanf( "%d", &NumCount );
  } while( ( NumCount < 1 ) || ( NumCount > 80 ) ); // loop until the user entered a valid amount

  for( I = 0; I < NumCount; I++ ) // loop to enter the numbers
  {
    printf( "Enter number[%d]: ", ( I + 1 ) );
    // The following scanf needs a pointer to float
    // 'Numbers' as an array of floats can be used as a pointer to float
    scanf( "%f", Numbers + I); // 'Numbers + I' is equivalent to '&Numbers[I]'
    if(BiggestNum < Numbers[I]) // Check if that's the biggest number 
      BiggestNum = Numbers[I]; // Sets the biggest number
  }
  CalcNum = BiggestNum; // Set the calculated number to the biggest
  printf( "\nSubtracting %f", CalcNum ); // Show what's done
  for( I = 0; I < NumCount; I++ ) // Now doing the subtraction
  {
    if(Numbers[I] != BiggestNum) // The biggest number will not be subtracted
    {
      printf( " - %f", Numbers[I] ); // Show what's done
      CalcNum -= Numbers[I]; // Subtract the number
    }
  }

  printf( "\nSubtract of the numbers entered is: %f\n", CalcNum );
  return 0;
}
Thanks for all ur helps :)
Topic archived. No new replies allowed.
Pages: 12