# Question in C++

Page 1 of 1

## 2 Replies - 1186 Views - Last Post: 04 June 2007 - 02:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=28919&amp;s=a533e4fb6d07346c0cada681f867ecd1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 faisaly

Reputation: 0
• Posts: 22
• Joined: 28-May 07

# Question in C++

Posted 04 June 2007 - 12:16 AM

Dear All,
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.

Is This A Good Question/Topic? 0

## Replies To: Question in C++

### #2 AmitTheInfinity

• C Surfing ∞

Reputation: 119
• Posts: 1,565
• Joined: 25-January 07

## 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 :
```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 Trogdor

Reputation: 15
• Posts: 627
• Joined: 06-October 06

## 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.