473,396 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

program to find consecutive int values to the sum of inputted number

2
This project will give you more experience on nested for loops and user-defined functions. The original project came from an IT company’s programming test where they were asking for the most efficient code to solve one problem. For this assignment we only focus on the correctness of our code and will not consider code efficiency.





Background



Some numbers can be expressed as the sum of n (n>=2) consecutive positive integers. For example:



15=1+2+3+4+5

15=4+5+6

15=7+8



Given a number, your program should be able to find out all the correct solutions, and print them out on the screen. Numbers within each solution should be printed in ascending order. If more than one valid solution found, all the solutions should be ordered according to the first number within each of them.

The program will first ask user to input a number, then it will use nested for loops to find and print out all the valid solutions for the number entered. Each valid solution should be output as a sum as done in the sample executable. The total number of valid solutions is also counted and output.
Feb 6 '07 #1
11 12377
Ganon11
3,652 Expert 2GB
Is this something you are trying to do, or a problem you are presenting to the community?
Feb 6 '07 #2
xctide
2
Is this something you are trying to do, or a problem you are presenting to the community?
just need help on how to get started
Feb 6 '07 #3
Hex63
4
ive looked over this description also and cant seem to come up with any sort of solution to come up with something to add consecutive intergers to equal the number that is inputted
Feb 6 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
Firstly you need to tackle the logic of this.

Conclusions:

You would always have to start checking at 1 where a true result is always found then you check starting at 2 and so on. Whether a true result is found or not you would still move to the next number. Checking would always stop when you reach the (inputted number / 2) +1 as the consecutive number to this will take you over the number.

Therefore you would run a For .. Loop starting at 1 and ending at (inputted number / 2) + 1.

Does this help?
Feb 6 '07 #5
Hex63
4
im really new at this and this is my first programming class so im not really sure how to do this, i do however understand the basic concept that if u divide a number by 2 and add 1 that no other consecutive numbers higher than that value can equate to the inputted number..is there anyway u can walk me through this
Feb 6 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
im really new at this and this is my first programming class so im not really sure how to do this, i do however understand the basic concept that if u divide a number by 2 and add 1 that no other consecutive numbers higher than that value can equate to the inputted number..is there anyway u can walk me through this
Sorry Hex

I'm only approaching this from the logic perspective. I'm not a C++ programmer but I can get someone else to look at it. Firstly you will have to attempt the code yourself. If you make any kind of attempt the experts will be a lot more willing to help you rather than just to provide you with the full program code. Make an attempt at designing the code yourself and then post the code here and I'll get some of the other experts to have a look.

Mary
Feb 6 '07 #7
Please look the below thread ..

http://www.thescripts.com/forum/thread597068.html
Feb 6 '07 #8
Firstly you need to tackle the logic of this.

Conclusions:

You would always have to start checking at 1 where a true result is always found then you check starting at 2 and so on. Whether a true result is found or not you would still move to the next number. Checking would always stop when you reach the (inputted number / 2) +1 as the consecutive number to this will take you over the number.

Therefore you would run a For .. Loop starting at 1 and ending at (inputted number / 2) + 1.

Does this help?

Instead of using for loop .... we can start this problem with recursion....because for a given number we initially dont know how many for loop should be there ..... so start wid recursion... for a iinputted number... start wid n=1 till (number/2)+1 take an array to store and print resuts.... use backtracking......if sum exceeds from the given number.......

correct m eif im wrong....

thanks n regards
rameshwar....
Feb 6 '07 #9
here is rough algo for given problem......



Expand|Select|Wrap|Line Numbers
  1. int number;               // inputted number declared as global variable
  2. int *arr=(int*)malloc(2*(number/2)+1); //inttialise all element to zero...
  3.  
  4. for(i=1;i<number/2;i++)
  5. {
  6.            sum=0;
  7.            j=0;                        // a  variable vl use for indexing array
  8.            flag=find_numbers(i,sum,*arr, j);
  9.            if(flag)
  10.            //print array elements upto j index .....
  11. }
  12.  
  13. int find_numbers(int i,int sum,int *arr,int j)
  14. {
  15.            if (sum>number)
  16.            return 0;  
  17.            if (sum==number)
  18.            return 1;  
  19.  
  20.            arr[j++]=i;
  21.            sum+=i;  
  22.             i++;
  23.             find_numbers(i,sum,*arr, j);
  24. }

plz correct me if i m wrong....
thanks n regards
rameshwar
Feb 6 '07 #10
Ganon11
3,652 Expert 2GB
I can see this problem being solved with only 2 for...loops and no recurrsive functions. You know you will need to check starting with every number between 1 and (number / 2) + 1. For each check, you need to find consecutive integers that will add up to the number - this is another for... loop. In the second loop, you will be adding the index (which will start at the first index and go until the number is reached) to an overall sum. If the sum equals the number, print the numbers and break from the second for... loop.
Feb 6 '07 #11
AdrianH
1,251 Expert 1GB
Break down the problem:
  • Create a function that will find if starting from i, will result in a consecutive set of numbers that will sum to n. Use a loop for this summing from i to n/2+1 or when sum equals or exceeds n. Return the final number in the sequence for printing purposes, if result is 0, no sequence was found.
  • Create a function that will print out the sequence of numbers adding to n.
  • Now call the first function in a loop from i=1 to n/2+1.
    • If it returns anything other than 0, call the print function. Alternatively, call the print function irregardless of the result and let it not print if the end value is 0.
    Why n/2+1? Because n/2+n/2+1=n+1, so there is no need to go beyond that point. You cannot use n/2 because n is a member of integers, so when n is odd, n/2+n/2=n-2 due to round off error. For n/2+n/2+1, I stated the worst case, best case is that it sums to n-1, and you stated that it had to be a sequence of consecutive summed values with your example not showing a sequence of 1 being a valid value.

Though I could write the code, I'll leave it as an exercise to the reader.


Adrian
Feb 6 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Neeraj Sharma | last post by:
Please Send Me The C program Which converts the decimal number to hexadecimal
3
by: vijaybaskar3108 | last post by:
hi, I just want to know how to find complements for a number. These are the following answers for complements 2's complement(10110)=01010 4's complement(1230)=2110 5's complement(4322)=0123...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
1
by: gouse | last post by:
Hi, Can any one tell me how to find "duplicate values exists in columns or table". please reply as early as possible. ThanQ
5
by: Larry__Weiss | last post by:
Can a C# program find out what language an assembly (or components thereof) is written in? - Larry
1
karthickkuchanur
by: karthickkuchanur | last post by:
how to find the seventh largest number in a column for example 1,2,3,4,5,6,7
2
by: star01daisy | last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.