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:
Anonymous2012-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.
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);
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?
targetSum = 30;
printf("The first and last numbers in every series of\n "
"positive consecutive integers whose sum is ");
printf("%d are: \n", targetSum);
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:
Anonymous2012-05-21 3:54
goddamn i never knew there were so many shitty programmers on here
>>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.
>>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:
Anonymous2012-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)
>>34
replace the filter and last map with
Seq.toList |>
List.choose id
never re-implement dumb shit like that... my bad guise
Name:
Anonymous2012-05-29 20:20
>>35
or just Seq.choose id
i have to read msdn more
Name:
Anonymous2012-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:
Anonymous2012-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