Nálaroth
Would you like to react to this message? Create an account in a few clicks or log in to continue.


A forum for the users to roleplay together in various settings, e.g. historical, fantasy, or sci-fi.
 
HomeHome  SearchSearch  Latest imagesLatest images  Register  Log inLog in  IRC channelIRC channel  

 

 Programmer's thread

Go down 
2 posters
AuthorMessage
Elynay
Admin



Posts : 107
Join date : 2010-10-24

Programmer's thread Empty
PostSubject: Programmer's thread   Programmer's thread EmptyFri Dec 17, 2010 10:40 am

As I know of at least one other beginning programmer here, this is the place to discuss programming in general and share simple programs with others.

Deza, here it is: it calculates the average of five numbers.

(C++)

#include <iostream>
using namespace std;

int main()
{
double prinum;
double secnum;
double trinum;
double quadnum;
double quinnum;

double result;

cout << "This program will calculate the average of five numbers you enter.";
cout << "\n\n";

cout << "Enter the first number.";
cout << "\n\n ";
cin >> prinum;
cout << "\n\n";

cout << "Now enter the second number.";
cout << "\n\n ";
cin >> secnum;
cout << "\n\n";

cout << "Now enter the third number.";
cout << "\n\n ";
cin >> trinum;
cout << "\n\n";

cout << "Now enter the fourth number.";
cout << "\n\n ";
cin >> quadnum;
cout << "\n\n";

cout << "Finally, enter the fifth number.";
cout << "\n\n ";
cin >> quinnum;
cout << "\n\n";

cout << "The average of the five numbers is ";
result = (prinum + secnum + trinum + quadnum + quinnum) / 5;
cout << result;
cout << ".";

return 0;
}





Last edited by Elynae on Fri Dec 17, 2010 11:10 am; edited 1 time in total
Back to top Go down
https://nalaroth.rpg-board.net
Deza

Deza


Posts : 100
Join date : 2010-10-31
Age : 28

Programmer's thread Empty
PostSubject: Re: Programmer's thread   Programmer's thread EmptyFri Dec 17, 2010 11:02 am

That seems quiet good. I'll make a ruby alternative soon.
In the mean time, my bady indented maths question machine.
Code:
def hello
      puts 'whats your name'
      name = gets.chomp
      puts "Hello ". + name
   end

def maths_question
v2 = 1 + rand(22)
      
      
      
v1 = 1 + rand(22)

correctA = v1.to_i + v2.to_i   
   puts "Whats the sum of #{v1} and #{v2} ?"
   youSAY = gets.chomp
   while youSAY != correctA.to_s
      puts 'Wrong, please try again.'
      youSAY = gets.chomp
   end
end

def maths_congrats
   puts 'Congrats, you got the correct answer!'
end


hello
puts 'Do you want to do maths questions?'
domath = gets.chomp
while domath == 'yes'
maths_question

maths_congrats
puts 'Do you want to do more?'
domath = gets.chomp
end
puts 'Well thats a shame, goodbye then.'
puts "Press enter to quit."
gets


Back to top Go down
Deza

Deza


Posts : 100
Join date : 2010-10-31
Age : 28

Programmer's thread Empty
PostSubject: Re: Programmer's thread   Programmer's thread EmptyFri Dec 17, 2010 1:27 pm

Okay, the alternative of Ely's program (in ruby).

Code:

# As you can see its much more simple.

puts 'Do you want to find the average of five numbers? (yes or no)'
yesno = gets.chomp

# If the person enters anything besides yes it goes strait to the "press enter to quit"

while yesno == 'yes'
puts 'Please enter your numbers, eg. "10 20 50 70 60"'

# Gets the five numbers and splits it into a array

nt = gets.chomp.split(" ")

# Adds the numbers in the array together

ntt = nt[1].to_i + nt[2].to_i + nt[3].to_i + nt[4].to_i + nt[5].to_i

#  Divides the sum of the array by five, to find the average.
 
nttt = ntt.to_i/5

# The #{nttt} is a quicker way of making the nttt variable into a sperate string.

puts "The average of your numbers is #{nttt}"
puts 'Do you want to go it again?'

# If the answer = yes then it returns the user to the beginning of the loop.

yesno = gets.chomp
end

#  To prevent the program from exiting to quickly I've made it so the user has to enter something.

puts 'Press enter to quit.'
gets
Back to top Go down
Sponsored content





Programmer's thread Empty
PostSubject: Re: Programmer's thread   Programmer's thread Empty

Back to top Go down
 
Programmer's thread
Back to top 
Page 1 of 1
 Similar topics
-
» Picture thread
» Logbook Thread
» The Minecraft Thread
» (I await approval or rejection of the idea itself before I reveal further information, see thread)

Permissions in this forum:You cannot reply to topics in this forum
Nálaroth :: Offtopic :: Creative-
Jump to: