vubaboota
Guest
Posts: n/a

 06-05-2007
I HAVE SOME SERIOUS PROBLEM , HOW TO MAKE A PROGRAM IN C
Q1:
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.

KINDLY HELP ME TO MADE THIS PRGRAM GUIDE ME OR GIVE ME CODE OF THE
ABOVE PROBLEM

Joachim Schmitz
Guest
Posts: n/a

 06-05-2007
"vubaboota" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) ps.com...
>I HAVE SOME SERIOUS PROBLEM , HOW TO MAKE A PROGRAM IN C

Please don't shout at us. (hint: a sentence in all capitals is considered
shouteing in usenet)
Seems you had a serious problem in the classroom, paying attention to your
teacher.

> Q1:
> 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.
>
>
> KINDLY HELP ME TO MADE THIS PRGRAM GUIDE ME OR GIVE ME CODE OF THE
> ABOVE PROBLEM

Try doing your homework yourself. Come back here if you have problems and
show what you've done so far.

Bye, Jojo

Flash Gordon
Guest
Posts: n/a

 06-05-2007
vubaboota wrote, On 05/06/07 07:44:
> I HAVE SOME SERIOUS PROBLEM , HOW TO MAKE A PROGRAM IN C
> Q1:

<snip>

> KINDLY HELP ME TO MADE THIS PRGRAM GUIDE ME OR GIVE ME CODE OF THE
> ABOVE PROBLEM

The best help anyone can give you is to tell you to attempt to do your
own homework. Well, that and DON'T SHOUT!
--
Flash Gordon

Chris Johnson
Guest
Posts: n/a

 06-05-2007
On Jun 5, 1:44 am, vubaboota <(E-Mail Removed)> wrote:
> I HAVE SOME SERIOUS PROBLEM , HOW TO MAKE A PROGRAM IN C
> Q1:
> 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.
>
> KINDLY HELP ME TO MADE THIS PRGRAM GUIDE ME OR GIVE ME CODE OF THE
> ABOVE PROBLEM

These problems seem relatively well described. Is there something
you're having trouble understanding? At least start on these problems,
yourself. That way, when you run into trouble, you can ask specific
questions and get useful answers. I wouldn't count on anybody doing

Keith Thompson
Guest
Posts: n/a

 06-05-2007
vubaboota <(E-Mail Removed)> writes:
> I HAVE SOME SERIOUS PROBLEM

Yes, you do. Do your own homework.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Umesh
Guest
Posts: n/a

 06-05-2007
//swapping

#include"stdio.h"
int main()
{
int a,b;
printf("Enter values of a & b : ");
scanf("%d%d",&a,&b);
a=a+b; //if a=4,b=2 now a=a+b=6
b=a-b; //now b=a-b=6-2=4
a=a-b;//now a=6-4=2
printf("After swapping\n a=%d \t b=%d",a,b);
return 0;
}

//table (allocate memory by yourself!)
#include"stdio.h"
void main()
{
int j=1,k=1,l=1;
int a;
printf("Enter a single digit number: ");
scanf("%d",&a);
printf("\nThe multiplication table of %d is:\n",a);
//formatting
for(l=1;l<=a;l++)
printf("\t%d",l);
printf("\n");
for(l=1;l<=8*a;l++)
printf("-");
printf("\n");
//result
while(j<=a){
printf("%d|\t",k); //formatting
for(int i=1;i<=a;i++)
printf("%d\t",i*j);
printf("\n");k++;
j++;}
}

mark_bluemel@pobox.com
Guest
Posts: n/a

 06-05-2007
On 5 Jun, 07:44, vubaboota <(E-Mail Removed)> wrote:
> I HAVE SOME SERIOUS PROBLEM , HOW TO MAKE A PROGRAM IN C

First point - don't type in CAPITALS. It is taken as SHOUTING and is
impolite. Many people will choose to ignore your post, or killfile you
(ensure that they don't see anything your post again).

Second point - this newsgroup is not "comp.lang.c.do.my.homework". We
are not here to make up for your lack of attention in class, your
incompetence. If you are learning, you have to make some effort
yourself - write some initial attempts at solving the problems set,
and then you can, politely, ask the group for assistance when you
encounter difficulties with what _you_ have written.

> Q1:
> 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?

Odd english in the question, but still it's fairly clear what is
required.

How would you get input from the user? (Hint: I'd recommend not using
scanf()).
How do you convert the input to a numeric?
How would you square the number?

Producing the output is trivial, surely. A loop to do the headings,
followed by a loop, with a nested loop for each row.

What have you tried? What problem did you encounter?

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

This was recently discussed in the group - a Google search would find
the discussion. It may also be discussed in the FAQ (Frequently Asked
Questions) at c-faq.com - it's so completely pointless and irrelevant
a task to what I do, that I haven't looked. I seem to recall the trick
involves XOR and I also seem to recall that there are shortcomings to
it, but as I say, I haven't spent a lot of time or energy on looking
into it.

Chris Dollin
Guest
Posts: n/a

 06-05-2007
Umesh wrote:

> int a,b;

> a=a+b; //if a=4,b=2 now a=a+b=6
> b=a-b; //now b=a-b=6-2=4
> a=a-b;//now a=6-4=2

Pointless pointless ungeneralisable pointless trickery, with the nice
possibility for undefined behaviour pointlessly thrown in for "free".

Now you've seen it, (a) never use it [1], (b) work out three ways why
it's a bad idea, (c) never use it.

[1] Unless there are utterly overwhelming reasons why you must; if there
ever are, come back here & tell us about them.

--
Is it a bird? It is a plane? No, it's: http://hpl.hp.com/conferences/juc2007/
WARNING. Various parts of this product may be more than one billion years old.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Guru Jois
Guest
Posts: n/a

 06-05-2007
On Jun 5, 1:17 am, Umesh <(E-Mail Removed)> wrote:
> //swapping
>
> #include"stdio.h"
> int main()
> {
> int a,b;
> printf("Enter values of a & b : ");
> scanf("%d%d",&a,&b);
> a=a+b; //if a=4,b=2 now a=a+b=6
> b=a-b; //now b=a-b=6-2=4
> a=a-b;//now a=6-4=2
> printf("After swapping\n a=%d \t b=%d",a,b);
> return 0;
>
> }
>
>
>

a^=b^=a; // could be still better than 3 statements.

But anyway Mr Umesh don't spoon feed askers. Don't think that we don't
self-dependents.

Bye
Guru Jois

Martin Ambuhl
Guest
Posts: n/a

 06-05-2007
Umesh wrote:
> //swapping
>
> #include"stdio.h"

^^^^^^^^^
Unless you have your own private version of stdio.h, you should use the

> int main()
> {
> int a,b;
> printf("Enter values of a & b : ");
> scanf("%d%d",&a,&b);
> a=a+b; //if a=4,b=2 now a=a+b=6

any pre-C99 compiler are just plain wrong.

1) Suppose a+b > INT_MAX. What do you think your code will do?
2) The OP wrote "Write a macros in which swap two number without using
3rd variable". What makes you think all numbers are ints?
3) This is not a macro.

The worst thing, of course, is not your program but the inane question.
The correct answer is "don't do that; don't even try."

> b=a-b; //now b=a-b=6-2=4
> a=a-b;//now a=6-4=2
> printf("After swapping\n a=%d \t b=%d",a,b);
> return 0;
> }
>
> //table (allocate memory by yourself!)

That's fair. Leave something for him to do himself.

> #include"stdio.h"

^^^^^^^^^
see above

> void main()

^^^^
This is silly. main returns an int in a hosted environment. There has
never been a standard for C in which main was defined as returning void.

> {
> int j=1,k=1,l=1;
> int a;
> printf("Enter a single digit number: ");
> scanf("%d",&a);

Quite apart from the highly vulnerable form of input, with no error
checking, the above needs a fflush(stdout); after the prompt. I/O on
stdin and stdout need not be synchronous.

Note that a is not checked for being a single-digit number.

> printf("\nThe multiplication table of %d is:\n",a);
> //formatting
> for(l=1;l<=a;l++)
> printf("\t%d",l);

^^
*Never* use '\t' in output designed to be read by humans. Display
devices can interpret '\t' is widely differing ways and you have no idea
when you are writing your program what '\t' will end up as on output.

> printf("\n");
> for(l=1;l<=8*a;l++)

^^
You pulled that '8' out of empty air. You have no reason to expect
that 8 is an appropriate choice.

> printf("-");
> printf("\n");
> //result
> while(j<=a){

Don't rely on the initialization at the top of the block here. The
for(; construct is a much better approach.

> printf("%d|\t",k); //formatting

^^
'k' is a superfluous variable. There is no information associated
with k not already present in the variable 'j'.

> for(int i=1;i<=a;i++)

^^^
This is illegal for pre-C99 compilers. For C99 compilers, it creates
a needless confusion by shadowing the already declared 'i' with block scope.

> printf("%d\t",i*j);
> printf("\n");k++;
> j++;}
> }

To the original poster: If you turn in "Umesh"'s code, you will get the
grade you deserve. You will not like it.