473,387 Members | 1,541 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,387 software developers and data experts.

algorithm

I have an assignment to write an algorithm to print the first 10 odd numbers and this is my attempt
Initialize the first counter to 1 (i=1)
Initialize the second counter to 0 (j=0)
second counter is equal to second counter plus first counter(j=j+i)
print the value of the second counter (print j)
if second counter is greater than 10 (if j>10)
stop
else if j is less than 10 (j<10)
Add 2 to the value of the second counter (j=j+2)
print j
Repeat steps 4 to 9 until the value of second counter is greater than 10

Is it correct?
Apr 30 '18 #1
2 1577
donbock
2,426 Expert 2GB
Second counter (j) holds the successive odd values. You terminate the loop as soon as j>10. That means you print the positive odd numbers that are less than 10: 1,3,5,7,9. That’s only 5 values.

You call i “the first counter” but its value stays at 1. It doesn’t count. What do you intend for it to do?

Your algorithm says what to do when j>10 and what to do when j<10. What should you do if j=10?

Does your assignment restrict what properties of odd numbers you’re allowed to take advantage of? For example, your algorithm takes advantage of the handy fact that successive odd numbers differ by 2. Another potentially helpful property of odd numbers is that the value of the nth odd number is (2*n)-1 (for n >= 1). On the other hand, if the assignment says the only thing you are allowed to know about odd numbers is that they have a remainder of 1 when divided by 2, then you should start from 0, test the remainder, increment by 1 and repeat until you find the 10th value that passes the test.
May 1 '18 #2
This reply is very useful.Thank you and here is my second attempt
Step 1:Initialize the counter to 0 (j=0)
Step 2:counter is equal to j plus 1(j=j+1)
Step 3:print the value of the counter (print j)
Step 4:if counter is greater than 20 (if j>20)
Step 5:stop
Step 6:else if j is less than 20 (j<20)
Step 7:Add 2 to the value of the counter (j=j+2)
Step 8:print j
Step 9:Repeat steps 4 to 9 until the value of counter is greater than 20
May 2 '18 #3

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

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
0
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
9
Rabbit
by: Rabbit | last post by:
Introduction The Advanced Encryption Standard is the algorithm that won the National Insitute of Standards and Technology's (NIST) search for a standardized encryption algorithm in 2001. In 2002, it...
24
Rabbit
by: Rabbit | last post by:
INTRODUCTION The Secure Hash Algorithm 2 is a series of cryptographic hash algorithms designed by the US National Security Agency (NSA) and published by the National Institute of Standards and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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,...

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.