Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C programming HELP!!

Name: Ahh 2012-05-21 1:48

How do I make the following code using C programming:
Create a program that has the following fields & capabilities:

fields:

type purpose
int Storage for the target sum

methods

name return type purpose parameter(s)
main void Gets the target sum from the user
and then calls getSeries. none
getSeries void Determines the first and last integers in EVERY series of positive, consecutive INTEGERS whose sum is the target sum. Output sent to System.out object. none

For example, if the target sum is 30, your program would output the following:
THE FIRST AND LAST NUMBERS IN EVERY SERIES OF POSITIVE, CONSECUTIVE INTEGERS WHOSE SUM IS 30 ARE:
4...8
6...9
9...11

Name: Anonymous 2012-05-21 2:12

Obvious ``intro to programming'' homework detected.  Figure out how YOU would do this as a human, then once you have an algorithm, translate it into C.

Name: Anonymous 2012-05-21 2:15

Read SICP.

Name: Ahh 2012-05-21 2:15

So...please don't make fun of me, this is what I have so far.. I am a super beginner at programming.

#include <stdio.h>
#include <stdlib.h>


 int targetSum;

int getSeries(void)
{

      targetSum = 30;
   printf("The first and last numbers in every series of\n "
         "positive consecutive integers whose sum is ");
        printf("%d are: \n", targetSum);

        int length;
        for(length = sqrt(targetSum); length > 0; length--)
         {
         if (targetSum % length == (length & 1) * (length / 2))
    targetSum = targetSum * 2 / length - (length - 1) / 2 + "..." + targetSum * 2 / length + (length - 1) / 2;
         printf("%d",targetSum);

        }

     return getSeries;
    }

Name: ahh 2012-05-21 2:23

i changed it a little bit but only "THE FIRST AND LAST NUMBERS IN EVERY SERIES OF POSITIVE, CONSECUTIVE INTEGERS WHOSE SUM IS 30 ARE:" shows. What did I do wrong?


#include <stdio.h>
#include <stdlib.h>
#include <math.h>


 static int targetSum;

int main(void)
{

        targetSum = 30;
        printf("The first and last numbers in every series of\n "
         "positive consecutive integers whose sum is ");
        printf("%d are: \n", targetSum);



}

int getSeries(void)
    {
       int length;
        for(length = sqrt(targetSum); length > 0; length--)
          {
         if (targetSum % length == (length & 1) * (length / 2))
     targetSum = targetSum * 2 / length - (length - 1) / 2 + "..." + targetSum * 2 / length + (length - 1) / 2;
         printf("",targetSum);

            }

    }return getSeries;

Name: Anonymous 2012-05-21 2:48

>>4-5

What in the actual fuck...?

Name: Anonymous 2012-05-21 2:48

from http://bytes.com/topic/c/answers/597068-adding-numbers-sequence-using-nested-loops

ENJOY

#include <iostream>
using namespace std;
 
int count = 0, input;
int main()
{
    cout << "Input an integer value: ";
    cin >> input;   
    for(int outer=1; outer < (input-1); outer++)
    {   
        int sum=outer;
    for(int inner=outer+1; inner < input; inner++)
        {                   
        sum+=inner;
        if(sum == input)
            {
            cout << "solution "  << outer << "..." << inner << endl;
              break;
            }
        }       
    }
}

Name: Anonymous 2012-05-21 2:51

Just stop. All of you. My eyes are bleeding.

Name: Anonymous 2012-05-21 2:55

>>7
Wow you needed to google it.  Please get out and never come back.

Name: Anonymous 2012-05-21 3:02

It was a good start...

Name: Anonymous 2012-05-21 3:05

>>7
Isn't that C+?

Name: Anonymous 2012-05-21 3:12

>>11
No its actually C#

Name: Anonymous 2012-05-21 3:20

oh. gotchyaa. :)

Name: ahh 2012-05-21 3:51

THANKS >>7
But I think I needed it in regular C

Name: Anonymous 2012-05-21 3:51

#include <stdio.h>

void getSeries(int target)
{
        printf("THE FIRST AND LAST NUMBERS IN EVERY SERIES OF POSITIVE, CONSECUTIVE INTEGERS WHOSE SUM IS %d ARE:\n",target);
        for(int numToTry=1;numToTry<=target/2;numToTry++)
        {
                int sum = numToTry;
                int endNum = numToTry;
                while (sum<target)
                {
                        endNum++;
                        sum+=endNum;
                        if(sum==target)
                        {
                                printf("%d...%d\n",numToTry,endNum++);
                        }
                }
        }
}
int main()
{
        int inp;
        printf("Input your number: ");
        scanf("%d",&inp);
        getSeries(inp);
        return 0;
}


compiles for me with "gcc -std=99". To make it compatible with C89 you'll have to forgo the implicit declaration in that for loop.

Name: Anonymous 2012-05-21 3:54

goddamn i never knew there were so many shitty programmers on here

Name: ahh 2012-05-21 4:08

>>15
IT WORKED. OMG
GOD BLESS YOU

Name: Anonymous 2012-05-21 4:23

>>15
Oops, meant "gcc -std=c99" but it looks like you're not using GCC anyway.

>>17
Heh, you're welcome.

Name: ahh 2012-05-21 4:34

>>18

I was using GCC i changed like two things and waala!
thanks again

Name: Anonymous 2012-05-21 12:01

>>1
You are an Indian.

Name: Anonymous 2012-05-21 12:20

for Xarn's sake use code tags

Name: Anonymous 2012-05-21 16:09

All play and no work makes Jack a wise man.

Name: Anonymous 2012-05-21 19:06

>>21
Did you hax my anus? I was wrote up an angrier version of that but decided not to post it because nobody else who is going to make this mistake in the future is going to see it and learn to use the fucking tag before they post.

Name: Anonymous 2012-05-22 8:16

bumpu pantsu

Name: Anonymous 2012-05-23 4:26

bampu pantsu

Name: Anonymous 2012-05-23 4:35

bampu pantsu

Name: Anonymous 2012-05-23 4:45

bampu pantsu

Name: Anonymous 2012-05-23 4:45

bampu pantsu

Name: Anonymous 2012-05-23 4:54

bampu pantsu

Name: Anonymous 2012-05-23 5:03

bampu pantsu

Name: Anonymous 2012-05-23 6:46

bampu pantsu

Name: bampu pantsu 2012-05-29 5:05

bampu pantsu

Name: Sgt.Kabu湿攔kimanҧ苌 2012-05-29 13:36

>>33
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
dubs won this time
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Name: Anonymous 2012-05-29 20:09

f# wins again - 1 compile only

let check n =
        let rec _check (x,y) = match (Seq.sum [x .. y]) with
                                                       | t when t > n -> None
                                                       | t when t = n -> Some (x,y)
                                                       | t when t < n -> _check (x, y+1)
                                                       | _ -> None
        Seq.map _check [for x in 1.. n/2 do yield (x,x)] |>
        Seq.filter (fun x -> match x with
                                    | None -> false
                                    | Some x -> true)|>
        Seq.map (fun (Some x) -> x)

printfn "%A" ((check 30))

System.Console.ReadLine() |> ignore

Name: Anonymous 2012-05-29 20:18

>>34
replace the filter and last map with

 Seq.toList |>
 List.choose id

never re-implement dumb shit like that... my bad guise

Name: Anonymous 2012-05-29 20:20

>>35
or just Seq.choose id
i have to read msdn more

Name: Anonymous 2012-05-29 20:26

after checking it, there were some mistakes
here is the updated version

let check n =
        let rec _check (x,y) = match (Seq.sum [x .. y]) with
                                                       | t when t = n -> Some (x,y)
                                                       | t when t < n -> _check (x, y+1)
                                                       | _ -> None
        Seq.map _check [for x in 1.. (if n/2 % 2 = 0 then n/2 else n/2 + 1) do yield (x,x)] |>
        Seq.choose id
printfn "%A" ((check 30))
System.Console.ReadLine() |> ignore

Name: Anonymous 2012-05-29 20:32

LOL NO ONE THOUGHT TO CHECK IF N WAS ODD FOR THE PEOPLE WHO USED N/2
what the fuck is wrong with you people

Don't change these.
Name: Email:
Entire Thread Thread List