For future reference:
http://cplusplus.com/forum/beginner/1/
Please ask an actual question next time.
I went through your code anyways, and fixed all the places I thought were problems.
I noticed a lot of your for loops and the counters were not working together. For example you subtract one from InputBuffer, that isn't necessary and happened to be a bug in your code because the for loops expected a different count. When I entered two the first time, it was changed to one. Then it gets to the for loop and I was set to zero and checked to if it was less than 1 and then executed once. Just be careful with how many times for loops go in the future and try to actually count their iterations. That is a popular bug.
There were a few other bugs in your program too and I do believe I commented them.
Hope this fresh code helps: (if not update your code and ask a proper question please)
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
|
#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 );// user input the subtract
//InputBuffer = InputBuffer-1;// deduct one number
} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );
//the next line was allocating size of int instead of float
Numbers = ( ( float * )calloc( InputBuffer, sizeof( float ) ) );// allow user decide how many numbers
if( Numbers == NULL )
{
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}
for( I = 0; I < InputBuffer; I++ )
{
printf( "Enter number[%d]: ", ( I + 1 ) );
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++ )
//this next line was setting j[80] = to numbers[i] then subtracting it from the total
//this would be an error if the user entered 81 numbers because that data would be lost
//I know this program only allows for between 1 and 80 but watch out for that in the future
//no reason for the extra memory storage use
Total -= Numbers[I];// subtract
printf( "\nSubtract of the numbers entered is: %f\n", Total );
return 0;
}
|
One more thing: I didn't do this for you, but you could combine the for loops 23, 30, and 34. If you did combine 23 though you would have to have another float variable for input because BiggestNum has to keep track of the biggest number if they were combined. Or better yet dodge the extra memory reference and just store it.
1 2 3 4 5 6 7 8 9 10
|
BiggestNum = 0.00; //this will only work if only positive numbers are entered
//otherwise the BiggestNum should be initialized to one of the numbers the user enters for the array
for( I = 0; I < InputBuffer; I++ )
{
printf( "Enter number[%d]: ", ( I + 1 ) );
scanf( "%f", &Numbers[I] );
if( BiggestNum < Numbers[I] )
BiggestNum = Numbers[I];
Total -= Numbers[I];
}
|
By the way if you did decide to go with negative numbers:
1 2 3 4 5 6 7 8 9 10 11 12
|
printf( "Enter number[1]: ");
scanf( "%f", &Numbers[0] );
BiggestNum = Numbers[0];
Total -= Numbers[0];
for( I = 1; I < InputBuffer; I++ )
{
printf( "Enter number[%d]: ", ( I + 1 ) );
scanf( "%f", &Numbers[I] );
if( BiggestNum < Numbers[I] )
BiggestNum = Numbers[I];
Total -= Numbers[I];
}
|
And unless you actually plan to use the variable BiggestNum later, I don't see the point in calculating it.