Dear All,

Please help me regarding these C++ Questions.

Q.1

Write a program using malloc function. In which you take input from user and allocate memory equal to square of this number. Which multiply numbers and draw a table in the following format?

Hint: User enters 3 then program allocates equal to 9 integer memories.

Output:1

Enter a single digit number:

2

The multiplication table of 2 is:

1 2

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

1| 1 2

2| 2 4

Output:2

Enter a single digit number:

4

The multiplication table of 4 is:

1 2 3 4

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

1| 1 2 3 4

2| 2 4 6 8

3| 3 6 9 12

4| 4 8 12 16

Q.2

Write a macros in which swap two number without using 3rd variable and then call

Macro in main function.

Hint: a=4, b=2 after swapping a=2, b=4.

## 2 Replies - 1271 Views - Last Post: 04 June 2007 - 02:09 AM

##
**Replies To:** Question in C++

### #2

## Re: Question in C++

Posted 04 June 2007 - 01:10 AM

Well It is expected that you show your efforts taken on the problem and then we help you if there is any problem.

still here is a starter for you

Q. 1 Allocating memory will not be a big deal for you as it is a malloc function and you can get it's syntax easily.

now about multiplication table. That is also not too difficult.

Logic would be :

Run a for loop from 1 to n [considering n as user input].

inside this loop have another loop which calculates tables.

something like this :

if you want to store this numbers into the memory you just now allocated then just remove that printf and put your variable there.

Q. 2 This was quite straightforward so here is the macro

still here is a starter for you

Q. 1 Allocating memory will not be a big deal for you as it is a malloc function and you can get it's syntax easily.

now about multiplication table. That is also not too difficult.

Logic would be :

Run a for loop from 1 to n [considering n as user input].

inside this loop have another loop which calculates tables.

something like this :

for(i=1; i<=n; i++) { for(j=1;j<=n;j++) { printf("%d ",(i*j)); } }

if you want to store this numbers into the memory you just now allocated then just remove that printf and put your variable there.

Q. 2 This was quite straightforward so here is the macro

#define SWAP(a,b) { a=a+b; b=a-b; a=a-b; }

### #3

## Re: Question in C++

Posted 04 June 2007 - 02:09 AM

You should not answer his questions by doing his homework while he showed no effort at all. Its not helping him, and its bad for the forum too.

Page 1 of 1