#include <stdio.h>
void main ()
int array[5];
int sum =0, I;
for (I=0; I<5; I++)
{
scanf("%d",&array[7]);
sum= sum + array[7];
}
i wan to do addition using array but the for loop seem to have problem:(
and i have no idea how to do a subtraction with array :(
help me thanks :)
Hi, I've looked at your code and noticed a few things. Firstly it would seem that you don't quite know how to manipulate arrays. In your code you have defined an integer array:
Frozendog11 wrote:
int array[5];
This creates an integer array of FIVE elements. Later you invoke that same array like this:
Frozendog11 wrote:
&array[7]
This is totally incorrect as you are trying to access the EIGHTH element of a FIVE element array (see the problem?).
I recommend that you complete some tutorials on arrays, this site has a number of good tutorials, and has one specifically for array. You can find it here: http://www.cplusplus.com/doc/tutorial/arrays/
You use arrays like you would any other data type. For example you have an array with two elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int sum;
int extra = 1;
int array[2]; //an array with two elements
array[0] = 1; //the index of an array ALWAYS BEGINS FROM 0
array[1] = 2;
sum = array[0] + array[1]; //equals 3
sum = array[1] - array[2]; //equals 1
sum = sum - extra; //equals 0
sum = array[2]; //trying to access the third element gives an error
//because it doesn't exist
I don't know enough about your problem is suggest impart any more advice.
Secondly, it is not good practice to use void main() for C++ applications. You should use:
1 2 3 4 5
int main()
{
//your code
return 0;
}
I found this explanation at Cprogramming.com:
Cprogramming wrote:
What's the deal with void main()
Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:
void foo(void);
A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.
Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.
#include <stdio.h>
#include <cstdlib>
#include <math.h>
int main ()
{
int option;
char repeat;
repeat:
system("CLS");
printf("*************************************\n");
printf("(1) Addition\n");
printf("(2) Subtraction\n");
printf("(3) Multiplication\n");
printf("(4) Division\n");
printf("(5) Optional function 1 (please name it)\n");
printf("(6) Optional function 2 (please name it)\n");
printf("(7) Optional function 3 (please name it)\n");
printf("(8) Optional function 4 (please name it)\n");
printf("(9) Quit\n");
printf("*************************************\n");
printf("Please Input Your Choice:");
scanf("%d",&option);
system ("cls");
if(option == 1)
{
int I, N;
float number,sum=0;
printf("How many numbers do you want to add:");
scanf("%d",&N);
for (I=1; I<=N; I++)
{
printf("Please enter number %d:", I);
scanf("%f",&number);
sum = sum + number;
}
printf("Total = %.2f\n",sum);
}
else
if(option == 2)
{
int G, J=0;
float number,subtract=0, number1;
printf("How many numbers do you want to Subtract:");
scanf("%d",&J);
{
printf("PLease Enter The Biggest Number To Be SubTract");
scanf("%f",&subtract);
for (G=1; G<=J; G++)
{
J=J-1;
printf("Please enter number %d:", G);
scanf("%f",&number);
subtract = subtract - number;
}
printf("Total = %.2f\n",subtract);
}
}
{
printf( "would you like to repeat? (y/n)");
fflush(stdin);//add code;
scanf("%c",&repeat);//true code;
}if(repeat == 'y'|| repeat == 'Y')
{goto repeat;}
else
return 0;
}
Is It possible to change it to array? i am bad @ array i need some guide :))
Goto although supported are not strongly recommended. Spare a thought for the next developer who need to maintain your program.
Is It possible to change it to array?
For array you need to know the fixed size in advance so it might be better to have a vector instead assuming you are allowed to use Standard C++ classes.
Based on my experience, depending on user to declare and you take their word for it you are going to invite problems for your program. What if they declare 5 but then enter 6 times, it will crash or corrupt your array isn't it ?
Command-line programs that take input from user via the console is very tricky to handle and sometimes I would prefer a GUI program instead to "curb" users wrong input :)
Please use [code]int x=5;[/code] tags around your code.
I just wondered, why are you using the cstdio library, rather than iostream? Is that an requirement for what you are doing? Or are you just reading an old book?
int main(void)
{
int numbers[80];
int count = 0,J;
long sum = 0;
float average = 0.0;
printf("Please Enter How Many Number You Want to Add");
scanf("%d",&J);// to get the number
count= count+ J;// determine the number of input number
int i;
for(i = 0; i < count; i ++)
{
printf("%2d",i+1);
scanf("%d", &numbers[i]);
sum += numbers[i];// add all the array
}
printf("\nSum of the numbers entered is: %f\n", sum);
return 0;
}
this is for the addition but it seem unable to run therefore i nidda help :(
1) You declare an array of 80, uninitialized integers.
2) You declare 2 int (J is uninitialized) objects, one long object, and one float object.
3) You tell the user to enter a number.
4) You take the given number and assign it to J. Let's assume J contains 91.
5) You assign count a new value which is the result of this compound expression: 0 + 91.
6) You declare another int object called i.
7) You then enter a for loop which is set to loop 91 times.
8) In your loop, you print the result of the compound expression, i + 1 to the screen.
9) You request user input. The value given is then stored into the ith element of numbers.
10) You add the value of the ith element of numbers to sum.
11) Your loop finishes and you print the value currently stored within sum to the screen.
12) You return from main( ) with the value of 0.
Step 9 is where you're going wrong. Your loop is set to loop 91 times (based on the users input at step 3/4). Since numbers can only store 80 integers, you write to memory locations that your program doesn't own (step 9). When you write to invalid memory addresses, the outcome of step 9 becomes volatile (unpredictable, undefined behavior).
so i have to declare J max number? like J[80]? (sic)
J isn't causing access violations, the for loop is. Like I said above, numbers can only hold 80 elements. Its size cannot be modified. So, when your loop reaches the 79th pass, you've reached the limit of numbers, and yet your loop keeps going. The latter causes an access violation (addressing memory that isn't yours).
Here's what you need to do:
1) Either dynamically allocate numbers based on the value given by the user in step 3/4, or, force the user to enter a number less than or equal to 80.
2) Initialize all objects prior to their use (important).
Using the steps above, you evaluate the number given by the user with a simple if statement. For example:
1 2 3 4 5 6 7
printf( "Please Enter How Many Number You Want to Add" );
scanf( "%d", &J );
if( ( J > 80 ) || ( J < 1 ) )
{
// Invalid input. 'numbers' can only hold 80.
}
Or, optionally, keep requesting input until a valid number is given. For example:
1 2 3 4 5
do
{
printf( "Please Enter How Many Number You Want to Add" );
scanf( "%d", &J );
} while( ( J < 1 ) || ( J > 80 ) )
Another option, as I've mentioned above, is to dynamically allocate numbers based on the users input. This is how you would do it:
1 2 3 4 5 6 7 8 9 10
int *Numbers = NULL;
printf( "Please Enter How Many Number You Want to Add" );
scanf( "%d", &J );
Numbers = ( int * )malloc( J * sizeof( int ) );
if( Numbers == NULL )
{
// Handle the failed allocation.
}
I also said that your for loop was causing access violations. I stand by this. Your loop should look like this:
1 2 3 4 5
int I = 0;
for( ; I < J; I++ )
{
// ...
}
If you choose to dynamically allocate numbers, make sure you check for NULL. If you don't and you enter the loop, you're asking for trouble.
int main(void)
{
int numbers[80];
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.
}
int i = 0;
for( ; i < J; i++ )
{
printf("%2d",i);
scanf("%d", &numbers[i]);
sum += numbers[i];
printf("%d",sum);
}
printf("\nSum of the numbers entered is: %f\n", sum);
return 0;
}
i am having some problem , the sum does not add up it onli add up @ the side so how do i print out the sum >.<