Question

Simple Macros In C

Include Print in the Macro Definitions

(When I try to run my solutions I get debugging errors)

Observe each of the functions below and understand their functionality. Next, convert each of the following functions to macros. Create a C file and test the macros that you have defined by writing a main function to call the functions and macros. Each macro is worth 3 points, including a main function which tests each macro is worth the remaining 3 points. Submit your program labeled as hw02q2.c [15 points] int square(int a) i return a * a; int add(int a, int b) return a + b; int min(int a, int b) if (a <b) return a; ) else t return b; void div(int a, int b) { print a/b; print NaN; if (b! e) { else

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

C program that demonstrates the macros
and functions of square, add, min
and div and print results to console.*/
//hw02q2.c
//header files
#include<stdio.h>
#include<conio.h>
//Macro defintions
#define SQUARE(a) a*a
#define ADD(a,b) a+b
#define MIN(a,b) a<b?a:b
#define DIV(a,b) b!=0?printf("%d",a/b):printf("%s","NaN");


//function prototypes
int square(int a);
int add(int a,int b);
int min(int a,int b);
void div(int a,int b);

int main()
{

   int a=5;
   int b=10;

   //calling function and macros
   printf("For a=5,b=10\n ");
   printf("Calling square function ");
   printf("square of %d is %d\n",a, square(a));

   printf("Calling SQUARE macro ");
   printf("square of %d is %d\n",a,SQUARE(a));


   printf("Calling add function ");
   printf("addition of %d and %d is %d\n",a,b, add(a,b));

   printf("Calling ADD function ");
   printf("addition of %d and %d is %d\n",a,b, ADD(a,b));


   printf("Calling min function ");
   printf("minimum of %d and %d is %d\n",a,b, min(a,b));

   printf("Calling MIN function ");
   printf("minimum of %d and %d is %d\n",a,b, MIN(a,b));

   printf("Calling div function ");
   div(a,b);

   printf("Calling DIV function ");
   DIV(a,b);

   //pause program output on console
   //until user press a key on keyboard
   getch();
   return 0;
}

//square function definition
int square(int a)
{
   return a*a;
}
//addition function definition
int add(int a,int b)
{
   return a+b;
}
//minimum function definition
int min(int a,int b)
{
   if(a<b)
   {
       return a;
   }
   else
   {
       return b;
   }
}
//division function definition
void div(int a,int b)
{
   if(b!=0)
   {
       printf("%d",a/b);
   }
   else
   {
       printf("%s","NaN");
   }
}

------------------------------------------------------------------------------------------------------------------

Sample Output screen shot:

For a-5.b-10 Calling square function square of 5 is 25 Calling SQUARE macro square of 5 is 25 Calling add function addition o

Add a comment
Know the answer?
Add Answer to:
Simple Macros In C Include Print in the Macro Definitions (When I try to run my...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first'...

    In C++ please. Thank you!! #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and 'last' is the last index void writeArrayBackward(const char anArray[], int first, int last) { int i = 0; for (i = last; i >= first; i--) { std::cout << anArray[i]; } std::cout << std::endl; } // test driver int main() { const char *s = "abc123"; writeArrayBackward(s, 0, strlen(s) - 1); } // Using the...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

    Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work...

  • CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...

    CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from row and // column wise sorted matrix #include<iostream> #include<climits> using namespace std;    #define INF INT_MAX #define N 4    // A utility function to youngify a Young Tableau. This is different // from standard youngify. It assumes that the value at mat[0][0] is // infinite. void youngify(int mat[][N], int i, int j) {     // Find the values at down and right sides of...

  • C++, Will Upvote: A) Try to get the following programs to run in your environment. B)...

    C++, Will Upvote: A) Try to get the following programs to run in your environment. B) If the programs will run, document each line of code with comments and describe any changes you had to make to the original code to get it to work. If the programs are unable to be modified to run with desirable results, explain why you think so. C) Description your solution and a synopsis of how your code is intended to work and the...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT