Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

erm, a little help here? ^^;

Options
  • 03-01-2003 2:24am
    #1
    Closed Accounts Posts: 76 ✭✭


    I am a C++ language user and I need your help guys to solve these two problems:

    #1

    Write a function that displays at the left margin of the screen solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays:
    ****
    ****
    ****
    ****

    #2

    An integer is said to be a perfect number if its factor, including 1 (but not the number itself), sum t the number. For example, 6 is perfect number because 6=1+2+3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect.



    I will really appreciate it if you guys can solve these two for me, even one is ok ^^;

    P.S. I use #include<stdio.h>, printf, scanf, switch [or knows are case] and while/do-while/for loop..


Comments

  • Closed Accounts Posts: 189 ✭✭Kenshin


    Originally posted by Zanza
    I am a C++ language user and I need your help guys to solve these two problems:

    #1

    Write a function that displays at the left margin of the screen solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays:
    ...

    [PHP]
    void square (int side)
    {
    for (int i = 0; i < side; i++)
    {
    for (int i = 0; i < side; i++)
    printf ("*");
    printf ("\n");
    }
    }
    [/PHP]
    and you call the function with something like square (4);


  • Moderators, Society & Culture Moderators Posts: 2,688 Mod ✭✭✭✭Morpheus


    are you sure Lord???

    I could be wrong (ive no compiler at home) but i think ur carriage return ("\n") may be inside the wrong "for loop"


    If Lord of Terror's code doesnt work, try this out
    My C++ is a bit dodgy, its been a while, out of work since August and im forgetting everything already :rolleyes:

    It should allow u to enter the size of side from a small DOS screen when u run the exe.

    let me know how u get on, i could almost swear my scanf is f*cked up its been so long since i used it... incidentally i remember doing this problem in my first or second year in Computer science in Dundalk IT way back in 96'!!!

    also try www.cplusplus.com for reference in future, and if this fails u may need a function definition before the int main() line? i really cant remember.

    #include<stdio.h>
    #include<conio.h>


    /* Copyright MaxJam developments 2003 (heh heh just kidding) */

    int main()
    {
    int side ;
    side = 0 ;

    printf("Please enter the size of side: ")
    scanf("%d",&side) ;
    printf("\n") ;

    square (side) ;
    }

    void square (int side)
    {
    for (int i = 0; i < side; i++)
    {
    for (int i = 0; i < side; i++)
    {
    printf ("*");
    }
    printf ("\n");
    }
    }


    happy coding! :)

    PS 2nd one is too complicated for this ungodly hour! il give it another looksee tomorrow!


  • Registered Users Posts: 1,328 ✭✭✭Sev


    Well here's my answer to your second query. It didnt take that long at all and it's also quite neat.
    
    #include "math.h"
    #include "stdio.h"
    
    bool isperfect(int f);
    int isfactor(int,int);
    
    int main(int argc, char* argv[])
    {
    	int i;
    	for(i=6;i<=1000;i++) {
    		if(isperfect(i)) printf("%i is perfect\n", i);
    	}
    	return 0;
    }
    
    bool isperfect(int f)
    {
    	int c = 0;
    	int i;
    
    	for(i=1;i<f;i++) {
    		c+=isfactor(i,f);
    	}
    
    	if(c==f) return true;
    	else return 0;
    }
    
    int isfactor(int f, int x)
    {
    	if((double)floor((double)x/f) == (double)x/f) return f;
    	else return 0;
    }
    
    


  • Closed Accounts Posts: 189 ✭✭Kenshin


    Originally posted by Morphéus
    are you sure Lord???

    I could be wrong (ive no compiler at home) but i think ur carriage return ("\n") may be inside the wrong "for loop"
    Yeah, I'm sure...
    Your code and mine do the same, it's just that I used less brackets :)
    I was too lazy to do the other, so gj Sev


  • Closed Accounts Posts: 5,025 ✭✭✭yellum


    College assignment ?


  • Advertisement
  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Originally posted by yellum
    College assignment ?

    Looks like it. The first question is so basic that if you can't do it on your own, then you have little hope of understanding it if people put it up for you.


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    Originally posted by yellum
    College assignment ?

    aye, looks like it's straight out of deitel and deitel iirc.

    Gav


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    If you're going to give printf examples could you give stream-based ones as well. Otherwise you're just encouraging bad habits.


  • Closed Accounts Posts: 76 ✭✭Zanza


    Originally posted by Morphéus
    are you sure Lord???
    I am pretty sure that LOT is sure, coz he did alot of my assignments ^^;
    Beside, Morph, you used this #include<conio.h>, we never used this one yet .. :)
    But thanx :)
    Originally posted by Hobbes
    Looks like it. The first question is so basic that if you can't do it on your own, then you have little hope of understanding it if people put it up for you.
    It is an assignmet, I am just being lazy at the first one, while the second was really hard..
    I know, I know that I am going to fail on this one.. --;
    Originally posted by Verb
    aye, looks like it's straight out of deitel and deitel iirc.
    heh, and yes it is!! :D

    Anyway, Thanx guys, thats all I wanted, beside, the first one is not finished, me thinks ^^;
    coz my first-timer-stupid-teacher wants us to put the function in a program, so its either LOT's one is completed in *his* way ;) or that I need to put it in a program..

    Thanx again guys :)


  • Closed Accounts Posts: 358 ✭✭CH


    I am a C++ language user
    lies!


  • Advertisement
  • Registered Users Posts: 410 ✭✭Drazhar


    zanza, your not in UL are you, i got those exact questions in a lab exam in second year.

    small question, why do a lot of programmers use printf, and scanf.
    personally i prefer cin and cout, but then thats just because i did C++, not C

    oh, that reminds me, sorry for throwing it here, is there a way to read from the command line in java?? im fairly sure there isnt, but if any one with more experience can prove me wrong, do


  • Registered Users Posts: 1,328 ✭✭✭Sev


    printf just seems more logical and straightforward to me, its a function. It takes parameters, returns a value and does its job. cout just looks stupid.


  • Registered Users Posts: 410 ✭✭Drazhar


    eh, fair nuff. I actually meant is it more advantageous (sp?) to use printf.

    Again, for me, cout makes perfect sense, "out", same with in. A first time programmer (very first time) might look at printf and assume it has something to do with a printer (which the screen is a form of i know)

    But anyway, just a random thought in a sea of useless ones that popped in to my head


  • Closed Accounts Posts: 189 ✭✭Kenshin


    Originally posted by Drazhar
    zanza, your not in UL are you, i got those exact questions in a lab exam in second year.
    She lives in Bahrain, in the middle east :)
    Originally posted by Drazhar
    small question, why do a lot of programmers use printf, and scanf.
    personally i prefer cin and cout, but then thats just because i did C++, not C
    Personally, I did lots of C before C++ so I'm just used to using it...
    Originally posted by Drazhar
    oh, that reminds me, sorry for throwing it here, is there a way to read from the command line in java?? im fairly sure there isnt, but if any one with more experience can prove me wrong, do
    Do you mean command line arguments?
    public static void main (String args[]);
    // args contains the command line parameters

    If you mean just read stuff in, like with scanf, then yeah, you can do that too...


  • Closed Accounts Posts: 1,719 ✭✭✭Ruaidhri


    Originally posted by Drazhar

    oh, that reminds me, sorry for throwing it here, is there a way to read from the command line in java?? im fairly sure there isnt, but if any one with more experience can prove me wrong, do

    ummm
    public class ArgsTest
    {public static void main(String args[])
    {
    .
    .
    .
    }
    }


    there's the key.args is an array of type string that keeps all parameters in it,taken from command line


  • Closed Accounts Posts: 76 ✭✭Zanza


    Originally posted by CH
    lies!
    *bows* sorry, I was just talking with my friend on the phone and it appeared that I am using C and not C++ ^^;

    Can I get any more stupid than that? --;


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    To echo what Hobbes said, lads, it's better to help someone figure out *how* to do a problem, rather then just give them the code, that helps no-one, whether it's a college assignment or not.

    For example, the second question could have been explained as in - find all the factors of the number, then add them all together. If this sum == the number, bang, you have a perfect number. Just giving the code, for example:
    int isfactor(int f, int x)
    {
    	if((double)floor((double)x/f) == (double)x/f) return f;
    	else return 0;
    }
    

    does nothing for him, because any newbie to programming would have no way of deciphering what's going on here. If it was done a little more long-winded and obtuse, as a newbie would, it would make more sense to them, and hence help them to learn. When people are learning code, it needs to be neither efficient or shorter, but simplistic enough for the leaner to understand.


  • Closed Accounts Posts: 14,483 ✭✭✭✭daveirl


    This post has been deleted.


  • Registered Users Posts: 1,328 ✭✭✭Sev


    Originally posted by daveirl
    How the hell do you get to having to do a programming assignment like that and not knowing even what language it is to be done in :rolleyes:

    Well most of the time I dont know wether im writing C or C++ specifically, if it compiles, im happy.
    Originally posted by Seamus
    Just giving the code, for example: does nothing for him, because any newbie to programming would have no way of deciphering what's going on here

    And seamus, I must just think differently to everyone else. Im not a fan of lengthy ugly comments interrupting, long winded illogical code. I find code easiest to understand when its short, simple, to the point and taking up least space on a document. It's easier to put everything into context mentally.

    Anyway, you learn best when you put in the effort to decipher, solve and understand something on your own accord rather than have it spoonfed to you. The satisfaction of that moment of clarity is what can make learning both fun and ensure you wont forget.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Originally posted by Sev
    And seamus, I must just think differently to everyone else. Im not a fan of lengthy ugly comments interrupting, long winded illogical code. I find code easiest to understand when its short, simple, to the point and taking up least space on a document. It's easier to put everything into context mentally.
    Sev your code was perfect, I'm not saying it should have been longer. But when you've got a newbie doing stuff and trying to decipher code like that, they'll never figure it out, and just go 'Ah **** it, it works, so I'm happy'.

    Anyway, you learn best when you put in the effort to decipher, solve and understand something on your own accord rather than have it spoonfed to you. The satisfaction of that moment of clarity is what can make learning both fun and ensure you wont forget.
    I agree. But I know from myself, that the more you just give the working code to people, the less they'll attempt to decipher it. In first year in college, I'd usually hint people on how to do something, or explain it verbally, because the people who I just gave code to, are the ones who came back, assignment after assignment, complaining that they don't know what to do and asking me to explain more and more complex concepts. A mate of mine managed two years and didn't know ho to write a class in Java. :rolleyes:

    I've done it myself, and I'm sure you have, from time to time; someone gives you a piece of code that you need and you just throw it in, cos **** it, it works, and that's all that's important. It helps not the learning :)

    Like your culliganism btw ;)


  • Advertisement
  • Registered Users Posts: 1,328 ✭✭✭Sev


    I'm just trying to make excuses for myself.
    Oh and I see youre another victim of Culligan Irish ;)


  • Closed Accounts Posts: 76 ✭✭Zanza


    Originally posted by daveirl
    How the hell do you get to having to do a programming assignment like that and not knowing even what language it is to be done in :rolleyes:
    lol, well, that's me!! :p


Advertisement