Array [Subtraction]

#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 ) );
Numbers = ( ( float * )calloc( InputBuffer, sizeof( int ) ) );// allow user decide how many numbers
if( Numbers == NULL )
{
printf( "Failed to allocate the array. Exiting" );
getchar( );
return 1;
}

{
printf("Enter Number[1]: ");
scanf("%f",&Q);
J[80] = Q ;// change to array
}
{
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];// subtract


printf( "\nSubtract of the numbers entered is: %f\n", Total );
return 0;
}
}
[Still having error] Please Help Thanks >.<
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.
Last edited on
Let said the output of the program should be 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
dont we need another array to mins that ?
I don't see a reason to have another array in your program to produce that type of output. Have you even wrote an outline yet for this programming problem? I think you should start with an outline and think of what variables and loops you can use to accomplish the task.
Hint: for that last line of output use a for loop that outputs each number and a subtraction sign.
how do i do that do u mind giving some guides?
how do i do a for loop to enter?
for(;Numbers[I]<=InputBuffer;Numbers[I]++)?
You could benefit from reading about for loops first: http://cplusplus.com/doc/tutorial/control/
If I had a list that I wanted to add all together, I would code something like this:
1
2
3
4
5
6
7
8
int total = 0;
for(int index=0; index<size-1; index++)
{
   total += array[index];
   cout << array[index] << " + ";
}
total += array[size-1];
cout << array[size-1] << " = " << total;
cout << isi it from <iostream.h>

The program will prompt the user for the amount of numbers to enter. Thereafter the program will prompt the user for the correct numbers to subtract, and display the result.(Do With Array)
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
Therefore i guess that J[80] would mins [-] All The number.. But i dun understand how a loop can break all the numbers
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
#include <stdio.h>

#include <stdlib.h>

 

int main(void)

{

float *Numbers = NULL, BiggestNum = 0,Total, J[81];

int InputBuffer, I;

 

do

{

printf("Please Enter How Many Number You Want to subtract(1~80): ");

scanf(" %d", &InputBuffer);

} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );

 

Numbers = ( ( float * )calloc( InputBuffer, sizeof( float ) ) );

if( Numbers == NULL )

{

printf( "Failed to allocate the array. Exiting" );

return 1;

}

 

printf( "Enter number 1: ");

scanf( " %f", &J[80] );
 

for( I = 0; I < InputBuffer-1; I++ )

{

printf( "Enter number %d: ", I+2 );

scanf( "%f", &Numbers[I] );

}

 

Total=J[80];

for( I = 0; I < InputBuffer; I++ )

Total -= Numbers[I];

 

printf( " Sum of the numbers entered is: %g ", Total );

return 0;

 

}

[This Is For Subtraction I suppose]
But When i change the Total-=Number[I]// calculation
To
Total/=Number[I] It didnt work well
bumpsssss
I suggest reading the tutorial on this site for arrays: http://cplusplus.com/doc/tutorial/arrays/
J[80] is the 80th entry of data in the J array.

cout is a class from the iostream library. More on that here: http://cplusplus.com/reference/iostream/
Of course you don't have to use cout, you can just use printf.

Also when you use calloc remember to include the free command. Ex: http://cplusplus.com/reference/clibrary/cstdlib/calloc/

So in the example outputs you have posted, I noticed that the biggest number always comes first. Is that how this program is supposed to have the data entered, or are those cases just coincidental?
Last edited on
thanks for all ur help err.... it is actually coincidental :)
anyway i learn a lot n it was use in my exam :)
Topic archived. No new replies allowed.