#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {
int count = 0;
do {
cout << "How many numbers will you be using? : ";
cin >> count;
} while (count <= 0);
/* Continue prompting until a positive number is entered */
float numbers[count]; //Create an array of float with size of count
for (int i=0; i<count; i++) {
cout << "Enter number #" << (i+1) << ": ";
cin >> numbers[i];
}
/* Fill the float array with numbers according to count */
float result = numbers[0]; //Set the result to the first number of the array which is number #1
for(int x=1; x<=count-1; x++)
{
result += numbers[x];
}
cout << "Addition result: " << result << endl;
result = numbers[0]; //Reset back result to the first number of the array which is number #1
for(int x=1; x<=count-1; x++)
{
result -= numbers[x];
}
cout << "Subtraction result: " << result << endl;
result = numbers[0];
for(int x=1; x<=count-1; x++)
{
result /= numbers[x];
}
cout << "Division result: " << result << endl;
return 0;
}
To Proskyslayer:
Ah , very good...your code should work :)
take note : calloc (amount of this variables, sizeof(variable)) returns (pointer with no type a.k.a void)*
remember to use sizeof (variable) to measure the amount of memory space needed to store the variable properly.
For debugging...
1. when you compile, look at the error code (if there is) and try to understand what it is telling you...
2. if the output is not what you expect...insert printf statement to list your variables. and try to work it out step by step...
optimise solution...
you can download the file from here http://www.4shared.com/file/qJHtrGQ5/testing.html
#include <stdlib.h>
{
float *Numbers=NULL ; // defines a pointer which will point to an array later
int InputBuffer, I;
/* ask user for the amount of number to operate on */
do
{
printf("Please Enter How Many Number You Want to subtract(1 to 80): ");
scanf(" %d", &InputBuffer);
} while( ( InputBuffer < 1 ) || ( InputBuffer > 80 ) );
/* allocate actual memory space as an array base on user input and have the pointer points to it */
Numbers = ((float*)calloc(InputBuffer+1, sizeof(float))); // stores the amount from user plus 1 more for operative
if( Numbers == NULL )
{
printf( "You have not enough RAM and I don't care...bye bye :p " ); // just my random text...
system("pause");
return 1;
}
/* Ask user for numbers to be operated */
for( I = 0; I < InputBuffer; I++ )
{
printf( "Enter number %d: ", I+1 );
scanf(" %f", &Numbers[I]); // store all input to the front of the array
}
/* The operative process */
Numbers[InputBuffer+1]=Numbers[0]; // load first number to the end of initialised array and operate from there
for( I = 1; I < InputBuffer; I++ )
Numbers[InputBuffer+1] -= Numbers[I];
/* Presentation of the equations */
printf(" %g" , Numbers[0]); // display first number in the equation
for( I = 1; I < InputBuffer; I++ ) // display subseqence numbers in the equation
{
if (Numbers[I]<0)
printf( "-(%g)", Numbers[I] ); // better presentation of minus by a negative number
else
printf( "-%g", Numbers[I] );
}
printf( "=%g ", Numbers[InputBuffer+1] ); // display final result in the equation
system("pause");
return 0;
}
Originally posted by Goldmonster:
you sure your code is acceptable for all version of c++ compiler? :)
Thanks for all ur helps :) :))