Join 131,670 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 3,679 people online right now. Registration is fast and FREE... Join Now!
I just did an assignment too quickly so I want to make sure that I did it right. It works properly but seems too easy to be true.
The assignment says to write a program that defines and uses macro SUMMARY to sum the values in a numeric array. The macro should recieve the array and the number of elements in the array as arguments.
I was not sure if I am supposed to somehow add the elements when I define it or as I did which works fine. Any advise is appreciated thanks.
int main() /* program main begins execution */ { int A[SIZE] ={1,2,3,4,5,6,7,8,9,10}; /* define array */ int i; /* counter */ int total = 0; /* initialize total to zero */
for (i=0; i< SIZE; i++ ) { /* compute total */ total += A[i]; } printf( "Total value of array is %d\n", SUMMARY ); /* print results */
return 0; /* indicates successful program execution */ }
This post has been edited by jsbeckton: 28 Nov, 2005 - 03:59 PM
Well, it depends on the intent of the assignment...you have used a macro here, but the way I read the assignment is they want that actual work done in a macro, as opposed to just using the macro as a replacement for a previously determined total.
Is it possible to use the for loop inside the macro? I have the Deitel book and our chapter on preprocessor is a whole 6 pages long, kinda left out examples for some reason.
It certainly is possible...a macro can house a function as well as a variable...it can accept parameters. Wherever the macro is encountered in the code, the definition of the macro is inserted.
I'll let you define the macro, but this is what I used as a main program:
CODE
int main() /* program main begins execution */ { int A[SIZE] ={1,2,3,4,5,6,7,8,9,10}; /* define array */ SUMMARY(A,SIZE); return 0; /* indicates successful program execution */ }
The output was
QUOTE
The sum of array elements is 55
You've already done everything you need...just put it inside the macro instead. You totally get the concept, it's just a matter of implementation. Let me know if you need a hand.
I tried to put the loop in the macro definition in () thats the way that thed did a simple example in the book but they did not have all of these variables or a loop, this is obviously not what you meant. How do i implement so much information in the definition?
CODE
#include <stdio.h> #define SIZE 10 #define SUMMARY (int i; /* counter */ int total = 0; /* initialize total to zero */
for (i=0; i< SIZE; i++ ) { /* compute total */ total += A[i]; } printf( "Total value of array is %d\n", SUMMARY );) /* print results */
int main() /* program main begins execution */ { int A[SIZE] ={1,2,3,4,5,6,7,8,9,10}; /* define array */ SUMMARY(A,SIZE); return 0; /* indicates successful program execution */ }
i implemented the macro all on 1 line as suggested but get 7 errors, did I miss something? Or did i leave something out when I modified it?
CODE
#include <stdio.h> #define SIZE 10 #define SUMMARY(A,SIZE) ({int i;int total=0;for(i=0;i<SIZE;i++){total += A[i];}printf("The sum of array elements is %d\n",total);})
int main() /* program main begins execution */ { int A[SIZE] ={1,2,3,4,5,6,7,8,9,10}; /* define array */ SUMMARY(A,SIZE); return 0; /* indicates successful program execution */ }
I'm sorry I meant visual stidio. I changed that and it executed correctly but when it compiles it gives a warning:
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(3) : warning C4005: 'SUMMARY' : macro redefinition c:\documents and settings\joshua\desktop\cit-145\ch_13.10.c(0) : see previous definition of 'SUMMARY'
That's peculiar...you are runiing this code exactly?
CODE
#include <stdio.h> #define SIZE 10 #define SUMMARY(A,SIZE) {int i;int total=0;for(i=0;i<SIZE;i++){total += A[i];}printf("The sum of array elements is %d\n",total);}
int main() /* program main begins execution */ { int A[SIZE] ={1,2,3,4,5,6,7,8,9,10}; /* define array */ SUMMARY(A,SIZE); return 0; /* indicates successful program execution */ }
I ran that under VS, no errors, no warnings, same version as yours...try starting a new project, and copy and paste this code in, see if it happens there as well.
Usually you see that if the macro has been redefined...like if you were supplying it on the command line, then defining it in code...or if it is another included file.
Failing that...try the code with no ; after the macro insert in the main function.