+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 27

Thread: Programming FUN!!!     submit to reddit submit to twitter

  1. #1
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    Programming FUN!!!

    rofl help me out someone who knows programming

    ok I need to make a program that calculates an average from the numbers i give it. and it needs to stop when I am done or I give it a negative number. so far i have this:

    #include<stdio.h>
    int main(void)
    {
    int n,counter=0,sum;
    float result

    n=o
    sum=0
    while(n>=0){
    printf("Numbers weeeeeeee:\n");
    scanf("%d",&n);
    sum=n+sum;
    counter=counter++;
    }

    result=(float)sum/counter;
    printf("The average of the numbers is: %.2f\n",result);
    }//endmain


    my problem is that the only way i can stop the cycle is to give it a negative number, can someone tell me what should I add to let the program know im done giving it numbers?

    thx so much :wink:

  2. #2
    Black Belt
    Join Date
    Jul 2004
    Posts
    5,745
    BG Level
    8
    FFXI Server
    Bahamut

    do your own homework

  3. #3

    this just a question but is it allowed to put a delimiting character into your string of numbers or was it meant to be stopped by the use of a negative number?


    what i mean:

    is this allowed: 1 2 3 -0

    the -0 would be used as your delimiting character that would be consumed and not used of course.

    so basically do you read until you use your own delimiting device such as a character of your choosing? many programs that are read input from keyboard can use something along the lines of input your whole string and then use the enter key as the way it stops. probably what you should do is a reading loop inputting a single number adding it to your group to average and ask each time after if that's the last number and use that yes or no as the condition that breaks out of your reading. just some ideas do what you see fit.


    by the way i see your using C functions the class C or your just used to C over C++? if given the option of using C or C++ for character input C has some nice validating options while C++ is easier function wise. i'm sorry if your aware of all this i just wanted to mention it.


    i graduated last may from Edinboro University of PA with a BS in computer science if you would like to ask questions feel free to ask at my school the base language you learned was C++ so i'm rather used to it

  4. #4
    Ridill
    Join Date
    Aug 2004
    Posts
    12,469
    BG Level
    9
    FFXIV Character
    Septimus Atumre
    FFXIV Server
    Gilgamesh
    FFXI Server
    Bahamut

    I know that actionscript and PHP are not the same a C++ (both are descendants of C), but this is how I would do it in actionscript...

    Code:
    function makeAverage(x) {
    	var count = 0;
    	var sum = 0;
    	if (x.length>0) {
    		for (var i = 0; i<x.length; i++) {
    			if (x[i]>=0) {
    				count++;
    				sum += x[i];
    			} else {
    				break;
    			}
    		}
    	}
    	if (count>0) {
    		return ("The average of the numbers is: "+(sum/count));
    	} else {
    		return ("No valad numbers were given.");
    	}
    }
    PHP would be fairly similar, except the variables would be denoted with "$", and I think array length is determined with "count()". Hope that helps.

  5. #5
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    Quote Originally Posted by Septimus
    I know that actionscript and PHP are not the same a C++ (both are descendants of C), but this is how I would do it in actionscript...

    Code:
    function makeAverage(x) {
    	var count = 0;
    	var sum = 0;
    	if (x.length>0) {
    		for (var i = 0; i<x.length; i++) {
    			if (x[i]>=0) {
    				count++;
    				sum += x[i];
    			} else {
    				break;
    			}
    		}
    	}
    	if (count>0) {
    		return ("The average of the numbers is: "+(sum/count));
    	} else {
    		return ("No valad numbers were given.");
    	}
    }
    PHP would be fairly similar, except the variables would be denoted with "$", and I think array length is determined with "count()". Hope that helps.


    that made me cry ; ;

  6. #6
    Ridill
    Join Date
    Aug 2004
    Posts
    12,469
    BG Level
    9
    FFXIV Character
    Septimus Atumre
    FFXIV Server
    Gilgamesh
    FFXI Server
    Bahamut

    Quote Originally Posted by I_am_new
    that made me cry ; ;
    In a good or bad way? O_o

  7. #7
    Relic Weapons
    Join Date
    Aug 2004
    Posts
    306
    BG Level
    4

    or do something like

    whle(scanf("%d", &number) != EOF)
    {
    counter += 1;
    othernumber += number;
    }

    result = other number / counter

    EOF being defined as CTRL + d in stdio.h, and hitting that would end the while loop and there ya go.

  8. #8
    Old Merits
    Join Date
    Jun 2005
    Posts
    1,010
    BG Level
    6
    FFXI Server
    Quetzalcoatl

    @.@;;;

    You. Are. All. Dorks. xDDD

    I understand NONE of that. >>';

  9. #9

    I undesrtand the concept, I just can't really figure it out since the only code I've ever done was LPC for a MUD :3

  10. #10
    Old Merits
    Join Date
    Jun 2005
    Posts
    1,010
    BG Level
    6
    FFXI Server
    Quetzalcoatl

    I can make pictures show up. :D:D:D

  11. #11

    Quote Originally Posted by Zigma
    do your own homework

  12. #12

    Uh...

    If you want it to stop when you're done, just say the way you denote you're done in the program's output is 'Input a negative number to finish.'

    That's how it's typically done in lower level CS classes...

    And what are you using C for? C++ onry

  13. #13
    Black Belt
    Join Date
    Jul 2004
    Posts
    5,745
    BG Level
    8
    FFXI Server
    Bahamut

    man, i knew the in's and out's of VB (and still do but it's a useless language now a days) back in the days but when i took a class in C, all my hopes of ever becoming a computer programer went flying out the window lol. Passed the class easily though lol but i could never dedicate myself to it.

  14. #14
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    Well back in High you could either take HTML or C/Pascal some crap like that. At the moment I wasn't entirely sure what I wanted for a life career. I took HTML.

    The last year of high, somehow they removed that class for w/e reasons and I couldn't take C anymore. Lucky for me my Uni teaches u C rofl xD or something, cuz I know poop bout programming. Anyway, I came to study Biomedical Engineering and it pwns. It's so hard at times tho x.x; but I'm enjoying Uni so bad. Anywho back on topic, so yah this semester we learn C and next semester C++. :3


    edit:
    ZOMG i got here and there was no one at the com labs, so i saw this machien and ran my uni card thru it :O such advanced tech for me.

  15. #15
    Relic Weapons
    Join Date
    Aug 2004
    Posts
    306
    BG Level
    4

    Ugh, I hate me CS department sometimes. I wont get to C++ til like second semester sophomore or junior year. Right now were doing C on Linux Debian... Its nice but is learning on a Linux platform REALLY gonna help us in a windows dominated world?

  16. #16
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    Yah, cuz ur language is the same, what changes is the compiler.

    Im also working on Antares. Sun Microsystems FTW

  17. #17
    Relic Weapons
    Join Date
    Aug 2004
    Posts
    306
    BG Level
    4

    Yea, using command line gcc is really gonna help when moving on to MS Visual Studio.

  18. #18
    Sea Torques
    Join Date
    Jul 2005
    Posts
    641
    BG Level
    5

    :3 u gotta start somewhere

  19. #19
    Relic Weapons
    Join Date
    Aug 2004
    Posts
    306
    BG Level
    4

    Bah! Humbug!

    <3 lasi btw =3

  20. #20

    Re: Programming FUN!!!

    Here's a few tips which will make your code a bit easier to read and also more efficient.

    Code:
    sum=n+sum;
    Change to
    Code:
    sum += n;
    Most (if not all) languages have +=, -=, *= and a few other operators which add (or subtract, multiply, etc) a value to a variable and store the result.

    Code:
    counter=counter++;
    Change to
    Code:
    counter++;
    the "++" operator already assigns the new value to the variable, there is no reason for the initial "counter=" in your code.

    I'm too rusty on C to tell you how to stop reading at the end of the input, so you'll need to figure that out on your own.


    Also, you won't be able to fully/properly utilize the features of an advanced programming language (or tools such as Visual Studio) without fully understanding the underlying principles of the language. The hardest part of programming is determining the logic equations that give you the desired results. It doesn't matter if you're programming console applications with unix compiler (gcc, g++, etc) or if you're using a nice graphical interface with automatic syntax-checking and debugging. If you can't determine the logic that gives you the desired result, then you won't be able to program it.

Similar Threads

  1. LOLs programming fun #2
    By Lasitha in forum General Discussion
    Replies: 7
    Last Post: 2005-10-04, 14:10
  2. Fun
    By Elcura in forum General Discussion
    Replies: 68
    Last Post: 2005-03-29, 03:14