EvilZone
Programming and Scripting => Projects and Discussion => : KoubaK May 07, 2013, 05:03:40 AM
-
Hey guys, so I recently started my Programing course, because I suck at it!
We are designing a program that will allow a user to input a list of your family members along with their age and state residency. Once the data is inserted into the script, the average age of the entire family would need to be displayed to the user. Once the Age is displayed, the names of those who live in the state of Texas would be listed.
Below is what I got so far... can anyone help me get this finished up.
//Main Module - Declare Variables
Declare Names[50] As String
Declare Ages[50] As Integer
Declare State[50] As String
Declare MemberName, Residency As String
Declare MemberAge, Count As Integer
Declare AgeAvg As Float
Call Welcome_Message module
Call Input module
End Program
//Input module - request necessary inputs
Set Count = 0
Write "Enter a family member's name; Enter * when done."
Input MemberName
While MemberName != "*"
Set Names[Count] = MemberName
Write "Enter this family member's age: "
Input MemberAge[Count]
Set Ages[Count] = MemberAge
Write "Enter this family member's state of residency: "
Input Residency
Set State[Count] = Residency
Set Count = Count + 1
Write "Enter a family member's name or * when done."
Input MemberName
End While(MemberName)
Call Age_Average module
Call Texas_Residents module
//Age_Average module - This submodule will produce the average family age
//and display it to the user.
Declare Sum As Integer
Declare K As Integer
Set Sum = 0
Set K = 0
//The While loop below will get the sum of all ages
While K < Count
Set Sum = Sum + Ages[K]
Set K = K + 1
End While
//The below computes the age average and set its corresponding value to the variable.
Set AgeAvg = Sum/Count
//Texas_Residents module - This submodule will determine which family
//members reside in Texas and display their names.
If State == "Texas" Then
Write "The following family members live in Texas: "
Write Names[50]
Else
Write "No family member lives in Texas."
End If
-
You forgot to mention what language is this. The syntax is not familiar to me.
-
It's really not a language at all, it is a pseudocode.
This is for a class I'm taking...
-
I will help you to finish your program as soon as I finish all of my unfinished programs :D
-
I will help you to finish your program as soon as I finish all of my unfinished programs :D
Hahahaha Okay... I shall wait :D in the meantime, if someone else has the time, that would be awesome.
-
Do you want someone to code it? Or just help you finish it? or what? I dont understand at all
-
Do you want someone to code it? Or just help you finish it? or what? I dont understand at all
I need someone to help me finish it. I'm having a hard time figuring out how to get he information to display only those who live in the state of Texas.
-
you already keeping track of the count, use a for loop to compare the "state[count]" variable with the word "Texas" if it finds it then display the name variable "Names[Count]"
what language you will code in ?
-
you already keeping track of the count, use a for loop to compare the "state[count]" variable with the word "Texas" if it finds it then display the name variable "Names[Count]"
what language you will code in ?
Rav3n, would the following be correct then?
For (State[Count] = 1; State[Count] == Texas; State[Count++])
Write "The following family members live in Texas: "
Write Names[Count]
End For
Well, this is my intro to programming, but if I get the time I will try to use Phyton or C+ so I can learn the language in the process.
-
You started the Count from "0"
& let's say it's in C++
for ( int x = 0; x <= Count; x++ ) {
if ( State[x] == "Texas" )
cout << Names[x] << "\n";
}
so we use array to get the value
I'm still learning C++ so don't take this as absolute working code but i think the idea is ok :)
Note if you get error, the "<=" might needs to be "<"
Also, add and else statement in case there was no match like displaying No Result was Found or something like that.
-
I went back to my textbook and I actually found my answer after some (okay, a lot of!!!) reading... I think the below will perform my task :D
Declare Index As Integer
Declare Found As Integer
Set Index = 0
Set Found = 0
While (Found == 0) AND (Index < Count)
If State[Index] == "Texas" Then
Set Found = 1
End If
Set Index = Index + 1
End While
If Found == 0 Then
Write "No Family members reside in Texas."
Else
Write "Following family members reside in Texas: "
Write Names[Index - 1]
End If
-
but what if there was more than one person with state is set to "Texas" ?
I think your code will just display the last one found.
You can use the While statement instead of the for, but i think it won't be as you posted.
From what i have read; for statement is used when you know exactly when the loop ends, in this case when it exceeds the Counts number.
But The while loop used when you don't know when the loop ends.
-
BTW, your code can work if you tweak it a bit.
Declare Index As Integer
Declare Found As Integer
Set Index = 0
Set Found = 0
While (Index < Count)
If State[Index] == "Texas" Then
Set Found = 1
write Names[Index]
End While
If Found == 0 Then
Write "No Family members reside in Texas."
The Found variable will be used to tell the last if statement to display no result msg if there was no match.
If you use if/else statement inside the while loop, the result will be like
No Family members reside in Texas.
No Family members reside in Texas.
KoubaK
No Family members reside in Texas.
Rav3n
it will display the message for every try.
-
Rav3n, good point... Thanks
So if I apply a For loop instead of the While... this is what I have in mind.
Declare K As Integer
Set K = 0
For (K = 0; K < Count; K++)
If State[K] == "Texas" Then
Set Found = 1
End If
Set K = K + 1
End For
Now, if the above works... how I get the data to display?
Do I simply invoke " Names[K] " ???
-
yes, but must make it inside the for loop,
and there is no need for the found variable
instead of set found = 1
make it write Names[k]
Also, you don't need to increment the K, K = K + 1
The K++ do that for you ( in C++ of course )
-
Rav3n, so it should look something like this:
Declare K As Integer
Set K = 0
For (K = 0; K < Count; K++)
If State[K] == "Texas" Then
Write Names[K]
Else
Write "No family members reside in Texas."
End If
End For
-
in pesudo code .. yes, but don't use else statement or you will end with the result i posted before.
In C++ , inside The for statement you can declare the variable and assign it to a value inside the for command.
-
Rav3n, so it should look something like this:
Declare K As Integer
Set K = 0
For (K = 0; K < Count; K++)
If State[K] == "Texas" Then
Write Names[K]
Else
Write "No family members reside in Texas."
End If
End For
No, that will still be wrong. Should look like this:
Declare K As Integer
Declare atLeastOnePrinted as boolean
Set atLeastOnePrinted = false
Set K = 0
For (K = 0; K < Count; K++)
If State[K] == "Texas" Then
Write Names[K]
Set atLeastOnePrinted = true
End If
End For
If !atLeastOnePrinted then
Write "No family members reside in Texas."
end if
bool atLeastOnePrinted = FALSE;
for(int i=0;i<=sizeof(States);i++)
{
if(States[i] == "Texas") {
cout << "Person: " << Names[i] << " lives in Texas." << endl;
atLeastOnePrinted = TRUE;
}
}
if(!atLeastOnePrinted)
cout << "No family members reside in Texas." << endl;
-
ande, since I cannot use a boolean (just because it has not been covered in class yet).
would introducing Found back into the pseudo work?
Declare K As Integer
Declare Found As Integer
Set Found = 0
Set K = 0
For (K = 0; K < Count; K++)
If State[K] == "Texas" Then
Write Names[K]
Set Found = 1
End If
End For
If Found == 0 then
Write "No family members reside in Texas."
end if
-
ande, since I cannot use a boolean (just because it has not been covered in class yet).
would introducing Found back into the pseudo work?
Declare K As Integer
Declare Found As Integer
Set Found = 0
Set K = 0
For (K = 0; K < Count; K++)
If State[K] == "Texas" Then
Write Names[K]
Set Found = 1
End If
End For
If Found == 0 then
Write "No family members reside in Texas."
end if
Yes, that would work just fine :) Basically a boolean is 1 and 0, true and false.
-
just replace
bool atLeastOnePrinted = FALSE;
with
int atLeastOnePrinted = 0;
AndatLeastOnePrinted = True;
with
atLeastOnePrinted = 1;
And if(!atLeastOnePrinted)
cout << "No family members reside in Texas." << endl;
with
if(atLeastOnePrinted == 1)
cout << "No family members reside in Texas." << endl;
In ande's Code
-
Thanks guys... Once I'm done with my other classes I will use the free time to try to actually code the entire program.