There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Basic Array/Function Programs


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Rhubes's Avatar
Junior Member with 11 posts.
 
Join Date: Oct 2003
12-Nov-2003, 11:37 PM #1
Basic Array/Function Programs
'Ello 'ello 'ello..(strong british accent)

I am needing to know how to write a program function that reads a list of positive integers (and ends the list with a negative integer) that is inputted from the user, and then deciphers the highest number out of the list...

I am also needing know how to print out that list of numbers in the opposite order of how the user inputs them...

normally it does..

User: 5 1 66 12 84 -2(end of list)

Output:
5
1
66
12
84

I need...

User: 5 1 66 12 84 -2(end of list)

Output:
84
12
66
1
5

Thanks and Danka-shane for any help you code wizards can give me
Rockn's Avatar
Computer Specs
Distinguished Member with 17,888 posts.
 
Join Date: Jul 2001
Location: Mexico of the North, MN
Experience: Disenfranchised American Male
13-Nov-2003, 09:48 AM #2
What are you trying to write this function in program wise?
Ironfxme's Avatar
Member with 51 posts.
 
Join Date: Oct 2003
15-Nov-2003, 06:57 PM #3
If your writing this in pascal which to me sounds possible as this looks like some sort of school course problem, right? And for some reason collages nowdays are hell bent on teaching pascal as a base, well anyway it's going to be pretty easy, you wontbe needing to write any arrays for this, it will be a case of IF ELSE END DO functions. Let me know like Rockn said what prog your wanting to write this in and I'll get back to you.
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
15-Nov-2003, 08:11 PM #4
Well let's not worry about the language then and just think in terms of pseudo-pseudocode as I like to think of it!

I am assuming you are aware of arrays as they are an easy way to store lists on numbers like you are contemplating. I also assume you are not familiar with dynamic arrays and pointers so we will simply set a limit to the maximum number of entries and make an array to hold that many. The extra unused elements will not be a problem.

I am also assuming that the user inputs the numbers one after the other individually and not as a line of text. If this is not the case you will have to first parse your line to separate out the individual integers.



Set MaxInts to highest number of integers acceptable

Create Ints[MaxInts] array to hold them all

Create Int MaxElementNo to represent final number of elements in array
Create Int ForwardsNo to represent position in array counting forwards
Create Int ReverseNo to represent position in array counting backwards
Create Int CurrentLargest to hold largest number that has been inputted
Create Int SwapInt to temporarily hold one element during the swap process towards the end


Initialise all variables to 0 including all members of array

Loop - While user input is not negative and you have not reached MaxElements
Input integer to Ints[ReverseNo]
If Ints[ReverseNo] is larger than CurrentLargest replace it
Increment ReverseNo


// Remember when we exit that loop ReverseNo now holds the number of entries and will point us to the final one

Set MaxElementNo to ReverseNo

// Now MaxElementNo holds the number of entries

Print out CurrentLargest

// Task one fulfilled!!!

Start at the beginning of the array

// ReverseNo holds the number of the last element and ForwardsNo holds 0, the number of the first element

Loop - While ReverseNo is greater than ForwardsNo

Swap Ints[ReverseNo] with Ints[ForwardsNo] using SwapInt
Increment ForwardsNo
Decrement ReverseNo


// If we reach a stage where ForwardsNo is equal to ReverseNo it means we had an odd number of elements and they have met at the middle element, so no swap is needed. If we reach a stage where ForwardsNo becomes larger than ReverseNo we had an even number of elements and after swapping the middle two adjacent ones the two placeholders have jumped over each other.

// We now have an array of the input numbers in reverse order.

Set ForwardsNo to 0

Loop - While ForwardsNo is less than or equal to MaxElementsNo

Print out Ints[ForwardsNo]
Increment ForwardsNo


// Second task fulfilled.



Think about what I have suggested and in particular try to see ways of making your final program more efficient than I have. If you need us to help with the code post back, but study what is here first!
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei

Last edited by AlbertB : 15-Nov-2003 08:21 PM.
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
15-Nov-2003, 08:28 PM #5
Just saw your post Ironfxme. Your idea of not using arrays is interesting and may be more efficient than mine if outputting the numbers is all that is needed, especially if Rhubes is not familiar with them.

I just assumed that holding on to the data and doing each step separately would be what was wanted at this level.

So should we boggle Rhubes with pointers and structures?
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
Snake~eyes's Avatar
Senior Member with 639 posts.
 
Join Date: Apr 2002
15-Nov-2003, 10:52 PM #6
Heres your answer in C++, this is untested but I think it should work.

Code:
int counter=-1;
cin >> number;
while(number > 0)//not sure if 0 woudl end the list or not?
{
      counter++;
      cin >> myArray[counter];
      
}
for(counter; counter > 0; counter--)
{
      cout << myArray[counter] << endl;
}
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
15-Nov-2003, 11:03 PM #7
Like I said, much more efficient, if you think it out first.

Why bother reversing the values in the array if it is not needed, simply output them in the correct reversed order. Dohhh!

But where is his largest integer in the selection Snake eyes?
Ironfxme's Avatar
Member with 51 posts.
 
Join Date: Oct 2003
16-Nov-2003, 04:31 AM #8
AlbertB, that's a good post mate he should get the idea from that all that I was thinking when I wrote my post was I didn't want to complicate things further for him, but your right on he has mentioned arrays and that might indicate he needs to use one... I like your idea Albert, seems as if it would be a solid program. hehe, maybe we should throw some pointers and structures in
AlbertB's Avatar
Distinguished Member with 2,432 posts.
 
Join Date: Nov 2002
Location: Hampshire, UK
16-Nov-2003, 06:12 AM #9
Hope you didn't think I was criticising. When I said "Dohhh!" I just meant "why didn't I see it like that?". Other people see things in Technicolor, I have to see things in colour and smell and sound and 6 dimensions!

Now Rhubes, start by declaring a class TSGmember and derive classes TSGQueryPoster, and TSGQuerySolver.........
__________________
1. "I make no personal claim to the truth, only the right to seek it, prove it in argument, and to be wrong many times in order to reach it."

2. "We have made a cage of words and placed our God inside, as boys trap a cricket, to make him sing for us alone."

Galileo Galilei
Ironfxme's Avatar
Member with 51 posts.
 
Join Date: Oct 2003
16-Nov-2003, 07:04 AM #10
Quote:
Originally posted by AlbertB:

Now Rhubes, start by declaring a class TSGmember and derive classes TSGQueryPoster, and TSGQuerySolver.........
LOL !! Great stuff. I know what you mean about seeing it in different ways, I guess that's the beauty of programming, so many different ways to go about one problem. Nice talkin to ya mate, have a good one.
Snake~eyes's Avatar
Senior Member with 639 posts.
 
Join Date: Apr 2002
17-Nov-2003, 11:10 PM #11
You're asbolutely right albert. Seems my code is totally flawed lol, but he shoudl get the general idea from what i wrote.
Mosaic1's Avatar
Distinguished Member with 7,498 posts.
 
Join Date: Aug 2001
18-Nov-2003, 06:00 AM #12
If the original Poster is still around, I may as well toss out the Visual Basic for this one. I used just a regular array to make it simple: ( no redimming)
Code:
Dim S(4) As Integer, i%, highest%
S(0) = 4
S(1) = 87
S(2) = 32
S(3) = 77
S(4) = -3
highest = S(0)

For i = 0 To UBound(S)

If S(i) > highest Then highest = S(i)
Next

 For i = UBound(S) To LBound(S) Step -1
 If S(i) >= 0 Then
 Msg = Msg & " " & S(i)
 End If
 Next

MsgBox Msg & vbCrLf & highest
Rhubes's Avatar
Junior Member with 11 posts.
 
Join Date: Oct 2003
20-Nov-2003, 12:57 AM #13
Wow thanks for all the help guys.. ..

unfortunately i had to hand in the assignment the next day and there were no replies by then.. so I somehow figured it out with help from classmates etc..

I appreciate the input though!
AcaCandy's Avatar
Computer Specs
Administrator with 98,717 posts.
 
Join Date: Jan 2001
Location: Las Vegas, NV & Acapulco, Mexico
Experience: Advanced
20-Nov-2003, 09:49 AM #14
For future reference, it's best if you do your OWN assignments, not just come here and ask us to do YOUR work for you
Snake~eyes's Avatar
Senior Member with 639 posts.
 
Join Date: Apr 2002
20-Nov-2003, 07:58 PM #15
Quote:
Originally posted by AcaCandy:
For future reference, it's best if you do your OWN assignments, not just come here and ask us to do YOUR work for you
I agree.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:47 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.