Welcome to Dream.In.Code
Getting C++ Help is Easy!

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!




Macros

2 Pages V  1 2 >  
Reply to this topicStart new topic

Macros, sum elements in array

jsbeckton
post 28 Nov, 2005 - 03:58 PM
Post #1


D.I.C Head

**
Joined: 8 Oct, 2005
Posts: 52


My Contributions


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.

CODE

#include <stdio.h>
#define SUMMARY (total)
#define SIZE 10



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
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 28 Nov, 2005 - 04:27 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


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.
User is online!Profile CardPM

Go to the top of the page

jsbeckton
post 28 Nov, 2005 - 06:25 PM
Post #3


D.I.C Head

**
Joined: 8 Oct, 2005
Posts: 52


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 28 Nov, 2005 - 07:49 PM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


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.
User is online!Profile CardPM

Go to the top of the page

jsbeckton
post 29 Nov, 2005 - 01:36 PM
Post #5


D.I.C Head

**
Joined: 8 Oct, 2005
Posts: 52


My Contributions


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 */
}


User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Nov, 2005 - 01:40 PM
Post #6


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


That is exactl what I meant, except a macro should all be on one line...that is implementing the function in a macro. Here is the macro I used.
CODE

#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);})
User is online!Profile CardPM

Go to the top of the page

jsbeckton
post 29 Nov, 2005 - 02:26 PM
Post #7


D.I.C Head

**
Joined: 8 Oct, 2005
Posts: 52


My Contributions


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 */
}
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Nov, 2005 - 03:29 PM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


Can you post the erros and tell me your compiler? I run that program with no probems.
User is online!Profile CardPM

Go to the top of the page

jsbeckton
post 29 Nov, 2005 - 03:39 PM
Post #9


D.I.C Head

**
Joined: 8 Oct, 2005
Posts: 52


My Contributions


visual basic c++ 6.9


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'
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(8) : error C2059: syntax error : '{'
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(8) : error C2143: syntax error : missing ';' before 'type'
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(8) : error C2065: 'i' : undeclared identifier
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(8) : error C2065: 'total' : undeclared identifier
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(8) : error C2059: syntax error : ')'
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(9) : error C2059: syntax error : 'return'
C:\Documents and Settings\Joshua\Desktop\CIT-145\CH_13.10.c(10) : error C2059: syntax error : '}'
Error executing cl.exe.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Nov, 2005 - 04:41 PM
Post #10


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


When you say visual basic c++, do you mean visual studio? Because if you do, the take the parentheses of the macro, and implement it like this:
CODE

#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);}
User is online!Profile CardPM

Go to the top of the page

jsbeckton
post 29 Nov, 2005 - 04:58 PM
Post #11


D.I.C Head

**
Joined: 8 Oct, 2005
Posts: 52


My Contributions


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'
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Nov, 2005 - 05:03 PM
Post #12


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


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.
User is online!Profile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/20/08 07:08AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month