expression without initialization

Hello mates....

lets say i have 3 int arrays and are initiated like

int a[]={A,B,1,0};
int b[]={C,D,0,1};
int add[4];
int A,B,C,D;
with A,B,C,D declared as integers but not initiated.
in a for loop i add a and b arrays and store the results in array add...
of course when i try to print the elements of the add aray i see only the numerical values to be added and some kind of huge numbers in the first two elements....(i would like to know what do these huge numbers represent)

So dont want to know the values of A,B,C,D nor their sum but i would like to have as a result from their addition an Alpharithmetic(A or B or C or D) or an integer....

for example i would like to declare
A+B=C
A+C=1
B+C=0
B*D=A
C*A=B
or any other combination of multiplication and addition

the compailer does not accept expresions like
A+B=1;or C+B=D; without initialized the values of variables first

i was wondering if that could be done usin the enum

Any help is welcome

thanks in advance
you could use macros for some of them, e.g.
1
2
3
4
int B,D
#define A (B * D)
#define C (A + B)


but this won't work for numbers unless you do something like

1
2
int A;
#define C (1 - A) 


It's not very elegant but it should work

You may be able to do something with overloading operators, a bit like smoke and mirrors though.
Last edited on
What you asking to do is not part of a procedural language mindset. You are thinking more along the lines of a functional programming language like Haskell.
2nd: The huge numbers you are seeing is uninitialized memory that has been allocated for your variables.


You cannot add A to B because A and B have no values. Therefore you end up with an invalid number. You are unable to say "C = A + B". C Must have a real value if it's going to be a normal type.

You COULD smoke and mirrors (thnx bnbertha) this. But it'd require you developing a bunch of classes that took pointers to real variables and performed a function on them, returning (through operator overloading) the value you asked for. But even then it'd need REAL numbers to work with.

One other thing with using #define
It's a preprocessor directive and is therefore global scope. They can cause weird compile and runtime problems unless you use very specific macro names.
Respected experts please chek my simple code and help me that in this code the division and modulus are not comming correct.

#include <iostream.h>

main()
{
//declaring variables//
int value1 , value2 , add , subtract , multiply , module , average , square;
float divide;

//asking the user for values //
cout << " Please enter the two numbers" << endl << endl;

// Getting values from the user//
cout << " Number one please" << endl << endl;
cin >> value1;
cout << " Number two please " << endl << endl;
cin >> value2;

//starting operations//

divide = value1 / value2;
add = value1 + value2;
subtract = value1 - value2;
multiply = value1 * value2;
module = value1 % value2;
square = value1 * value1;
average = value1 + value2 / 2;

//printing output for Addition//
cout << "The addition of " <<value1 << "and" <<value2 << " is" << endl << add <<endl;

//printing output for Subtraction//
cout << "The Subtraction of " <<value1 << "and" <<value2 << " is" << endl << subtract <<endl;

//printing output for Multiplication//
cout << "The Multiplication of " <<value1 << "and" <<value2 << " is" << endl << multiply <<endl;

//printing output for Division//
cout << "The Division of " <<value1 << "and" <<value2 << " is" << endl << divide <<endl;

//printing output for Modulus//
cout << "The Modulus of " <<value1 << "and" <<value2 << " is" << endl << module <<endl;

//printing output for Square//
cout << "The Square of " <<value1 << "and" <<value2 << " is" << endl << square <<endl;

//printing output for Average//
cout << "The Average of " <<value1 << "and" <<value2 << " is" << endl << average <<endl;



}
Division problem is operator precedence divide is done before addition so it is doing
 
average = value1 + (value2 / 2);


fix it by adding parenthesis to override operator precedence...
 
average = (value1 + value2 )/ 2;


Modulus looks OK so not sure about that one, as long as value1 > value2
Last edited on
first of all i would like to thank you for your answers.....
I knew i could not do what i wanted.....but i hoped for the existance of any way to do this i might not know.....the truth is that these alpharithmetics do represent integer trains but that is the hard way for what i want to do.....anyway

something else...is there a way when printing an int array instead of getting the address of the alpharithmetics to just get the alpharithmetic character on my screen?and if not is there any array type that could work as an int and char array at the same time.....
what i meen is to have the ability to make numerical calculations and also have the ability to print the alpharithmetic chars as they are without conversion of the array type....

thanks again mates
Mann: please create your own topic instead of hi-jacking others.

Zourlas: Ok, You CAN actually do what you wish to achieve. However, this would require a complete object-orientated parent-child hierarchy. You would need to define your own type and then sub-types.

E.g. SubType "Add" would take pointers to 2 other classes and add the values together on the fly when asked for the result.

This would require some careful planning, but it's actually possible to do it using a lazy execution pattern.

As for your last reply, You shouldn't ever get the address of the int but the value. The reason it looks like you are getting an address is because you have not initialized your int to something. When you create a new int (and don't give it a value) it will have some memory assigned for it to store it's value. The OS/Compiler does not ensure this memory is cleared before it's given to you. This is why you end up with strange values. You need to give you int's an initial value (e.g int A = 0; to ensure you don't end up with unexpected values.

e.g.
1
2
3
4
5
6
int A[] = {0, 1, 2};
for (int i = 0; i < 3; ++i) 
 cout << "Value " << (i+1) << " = " << A[i] << endl;

int B[] = new int[10]; // All may contain invalid values.
memset(B, 0, 10); // Set all 10 Elements to 0 



Bump. Just bumping this because I got asked how to do something similar at work. Weird how people want to emulate the functionality of a functional language in a procedural language.

Lazy Evaluation pattern ftw.
http://www.cplusplus.com/forum/articles/2473/

I have made a small posts that illustrates how you can do this :) Enjoy.
Topic archived. No new replies allowed.