Question in C++

Need Help in C++

Page 1 of 1

2 Replies - 964 Views - Last Post: 04 June 2007 - 02:09 AM Rate Topic: -----

#1 faisaly  Icon User is offline

  • New D.I.C Head

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

Question in C++

Post icon  Posted 04 June 2007 - 12:16 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Question in C++

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 109
  • View blog
  • Posts: 1,530
  • 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; }


Was This Post Helpful? 0
  • +
  • -

#3 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1