Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] How to get the sum of the ages (pascal)


  • Please log in to reply
13 replies to this topic

#1 marreco

marreco

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:Delphi/Object Pascal

Posted 07 October 2012 - 05:28 AM

Hi.

I need to get the sum of the ages.

Program Aula11_Exc01;
Var
idade:Array[1..5] of Integer;
soma, i:Integer;

Begin
	 For i:= 1 To 5 Do
Begin
Writeln('Enter the age...',i);
	 Readln(idade[i]);
	 End;
	 soma:= i + soma;
	 Writeln('The sum of the ages is ',soma);
	 Readln;
End.

can anyone help me?

thank you!
  • 0

#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 07 October 2012 - 06:32 AM

You need another loop to sum the content of idade array. Or you can do the summing inside the first loop right after you got the input. Just don't forget to "reset" soma variable to 0 prior entering the loop.
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 October 2012 - 10:51 AM

Any reason why you're storing the ages in an array, rather than just summing them as you go?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 marreco

marreco

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:Delphi/Object Pascal

Posted 07 October 2012 - 01:34 PM

Hi.
I have no concept of logic programming,'m apredendo.


it was an exercise.

I could not fit it.

Do I need an algorithm using vectors, which receive and store the age of 5 persons and at the end shows the sum of all ages entered.
  • 0

#5 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 07 October 2012 - 02:24 PM

If you're looking at this
soma:= i + soma;


I see that you're adding i instead the array of i. Make sure it's the array of i, that should solve your issue.
  • 0

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#6 marreco

marreco

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:Delphi/Object Pascal

Posted 07 October 2012 - 02:31 PM

Hi.

and how do I do it?
  • 0

#7 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 October 2012 - 03:37 PM

A typical approach is to have two variables: input and sum. You initialize sum to 0, then start reading ages into input inside a loop. For each input, you set sum:= sum+input.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#8 marreco

marreco

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:Delphi/Object Pascal

Posted 07 October 2012 - 03:46 PM

WingedPanther, you could send me an example?

I'm beginner in pascal.

Thank you!
  • 0

#9 VNFox

VNFox

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 648 posts
  • Programming Language:C#, PHP
  • Learning:Assembly

Posted 07 October 2012 - 11:35 PM

Here right here:

For i:= 1 To 5 Do
Begin
Writeln('Enter the age...',i);
		 Readln(idade[i]);
		 soma:= idade[i] + soma;
End;

Writeln('The sum of the ages is ',soma);

something like that
  • 1

www.pickmike.com
I don't just develop software. I find solutions to your business needs.


#10 marreco

marreco

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:Delphi/Object Pascal

Posted 08 October 2012 - 02:40 AM

Hi.


now it worked!!

thank you very much!!
  • 0

#11 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 08 October 2012 - 04:19 AM

Hi.


now it worked!!

thank you very much!!

Do you understand the difference between your code and VNFox code?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#12 marreco

marreco

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:Delphi/Object Pascal

Posted 08 October 2012 - 02:10 PM

hi, Orjan
I noticed that the structure put in 'For' ... if you want me explain, I'll be grateful!!

But I gotta tell you something.
1st I do almost nothing Logic programming, I'm trying to learn.

2nd I live in Brazil and I do not know English.

and how do I put this post as 'Solved'??
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download