[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2080
  • Last Modified:

Arrays

I have an assignment and need some guidance please.  I am new to programming so please take that into account.  Thank you.

Design a program that will allow a user to Input a list of your family members along with their age and state where they reside. Determine and print the average age of your family and print the names of anyone who live in Texas.
0
bcg_81
Asked:
bcg_81
  • 12
  • 7
  • 5
  • +1
5 Solutions
 
user_nCommented:
At what language this should be written? What exactly you now about programing?
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Independent of your programming skills, I always write programs / routines in pseudo code first. Pseudo code is independent of the language you are using to write the application in. This is your design.

So I'd start with 3 pieces of pseudo code or design. One is for getting the input / collecting your family members. Two is determining and printing the average age. Three is determining and printing who is living in Texas.
0
 
bcg_81Author Commented:
Well, Its confusing because we code in C++ but we use Raptor.  I know that not all C++ code is useful in Raptor.  

So I would start
Declare Names[100] string // I would need to set all three within a loop and set index= index +1??
Declare Location[100] string
Declare Age[100] as integer
Call Compute_Average

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
user_nCommented:
I do not know Raptor.
All this could be done with class or in structure. The result could be written to a file. And than you can read the information from the file and do the needed changes.

http://en.wikipedia.org/wiki/C%2B%2B_classes
http://linux.die.net/man/3/fopen
http://www.learn-programming.za.net/programming_c_learn10.html
http://www.cplusplus.com/reference/clibrary/cmath/
0
 
bcg_81Author Commented:
I just need to write the pseudocode.  Once I write out the code, would you be willing to look over it and  give me pointers?  Thank you very much!
0
 
user_nCommented:
http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29


struct member{
   string name[100];
   string Location[100];
   int Age[];
};

struct
family{
  int members; //num of members;
  member * array;
};

You should firs receive the numbers of family and  write it to numbers;
then in the code

 family familyarray;  
 array = new familyarray->array[members];

Then in cicle from 0 to family.members -1 you will enter the information for the members of the family
failyarray->array[index].name
failyarray->array[index].Location
failyarray->array[index].age


This can be done with a text file or from the console. It's up to you to decide but I think that it will be better with a text file
0
 
bcg_81Author Commented:
I just submit it in word. Why is there not a [100] for int age?  I think becsuse you are a master programmer that you over estimate my knowledge level.   I am sure that your code is correct but I need to understand what you have wriiten down so please be patient with me. I also knwo that you are not a miracle worker wither! :)  Im used to seeing code look like this

Main Module

 

 

Declare Member[10] as String  //Create Arrays

Declare Money[10] as Float

Declare totalSaving as Float

Declare mostSavings as integer


       For(i=0; i<10; i ++)

{

   Write “Enter Name “

   Input Member  // This is where Names are input

   Write “Enter Savings"

   Input Money
//This is where Savings is input.

}

 

Call Module Total_Savings  // Subprogram is called to calculate total savings.

Call Module Most_Savings  // Subprogram is called to calcualte Most Savings.

Write " Total Savings" + x  // Output will display total Savings.

Write " Total Savings" + total

Write "Name of Person" +[index] //Displays person with the most savings.

 

0
 
bcg_81Author Commented:
struct  // Struct is a variable?
family{
  int members; //num of members;
  member * array; //Why member * array?
};

You should firs receive the numbers of family and  write it to numbers;
then in the code

Are you saying that a function should be done that is not included in the actual main module?
0
 
user_nCommented:
My mistake
int age;
Every member of the family has just one age I suppose, the current age;

The sturcture family describes the hole family. The member "members" describes the number of members of the family. Each memeber of the family is described in the structure member with his name, age and location. In structrure family there is a dynamic array of the memebers of the family because I don't know how much are they. In family there is a dynamic array of the members of the family each described with name, age and location.
0
 
user_nCommented:
member * array - In family there is a dynamic array of the members of the family each described with name, age and location.
0
 
TommySzalapskiCommented:
user_n may be making this more complicated than it needs to be. I assume, from your questions that you have not learned about structs or dynamic memory.

Since this is for an assignment, you shouldn't be using stuff that hasn't been taught yet and you don't need any of that anyway.

Start with the code that you have in your second to last post. It's all good.

Is there something in the assignment about money?. You do need an array for the ages, put that back in.
You should keep track of all the totals in the for loop. As you read in each age, add it to the total so that at the end you have all the totals.

Then you'll need another for loop at the end with a simple if statement to output all the Texans.

Let's see what you come up with for that.
0
 
TommySzalapskiCommented:
Oh, have they told you to make sure the input is valid or are you assuming it will be?
0
 
bcg_81Author Commented:
You are correct,  I have not covered what user_n discussed but I appreciate his comments.  The code i showed you was from my last project, which I was lost.  Ok, Thanks!!!!  I will right it up withing the next couple of days and let you take a look at it if you dont mind.  Thank you very much Tommy! and anyone else who responded.  Please come back later!  Thanks again ~ Brian
0
 
bcg_81Author Commented:
Declare Names[100], Location[100] as string
Declare Age[100] as int
Declare AvgAge as float

Main Module

      
      For (i = 0, i < 100, i ++)
      
      {

            Write "Please input family data!"
      
            Write "Name"
            
            Input Name
      
            Write "Age"
      
            Input Age
      
            Write "Location"
             
            Iput Location

      }

Call Avg_Age sub program
Call Texas_ sub program

Output "AvgAge"
Output "Texas"

End


Sub program Avg_Age


For ???

Ok, I confused on how to set up a loop to calcualte the avg.  I know that by adding them up and dividing by the number that ill get the avg but how would thi sbe written in pseudocode?

While ( i=0, i <100, i ++)
 while ??


      

            
0
 
TommySzalapskiCommented:
You just keep a running sum
sum = 0
for i from 0 to 99
  sum = sum + Age[ i]

or something like that
0
 
bcg_81Author Commented:
So,

Call Sub program Avg_Age

Declare Sum as int
Delclare avg as float
 For (i=0, i< 100, i ++)

     sum = sum + Age
 AvgAge = sum/10

End Sub Program

Can you tell me how things look overall so far?  Any pointers? Anything that looks wrong?



     

     


0
 
bcg_81Author Commented:
I would need to set Avg to 0 correct?


Declare Sum as int
Delclare avg as float
set AvgAge to = 0
 For (i=0, i< 100, i ++)

     sum = sum + Age
 AvgAge = sum/10

End Sub Program


0
 
bcg_81Author Commented:
For finding the Person who lives in Texas......

Im a little puzzled here.

I know we need to look at Location

Could we say...

Call Texas_ sub program

Declare j as string
Set j = Texas
 For (i=o, i !=j, i ++)
   
So I set j = Texas and I need to get the name of the family member who lives in Texas.  I must compare name[100] to Location[100].  Im confused here.

Im stuck here.  I also have to consider if no one lives in Texas correct?





 
0
 
TommySzalapskiCommented:
For the average:
Set sum to 0, not AvgAge.

As soon as you run the line
AvgAge = sum/100     (100 not 10, right?)
Then it doesn't matter what AvgAge was.

But when you do
sum = sum + Age
Then it does matter what sum was before. So you need to make sure it starts at 0.


For the Texas part, you are just looking for people who live in Texas so your for loop will be just like before
for (i=0, i< 100, i ++)

Then you just need an if for if Location[i ] == Texas.
Then you could output Name[i ]

Yes, it would not be a bad idea to keep a count so you know at the end if you found any and can output something useful.

I'll let you put all the pieces together.

0
 
bcg_81Author Commented:
Tell me what you think about this please.  Also, Im questoning my output " Texas" int he main module. In order for that to output Texas, it must be declared correct.  I think that might be wrong!

Declare Names[100], Location[100] as string
Declare Age[100] as int
Declare AvgAge as float

Main Module

      
      For (i = 0, i < 100, i ++)
      
      {

            Write "Please input family data!"
      
            Write "Name"
            
            Input Name
      
            Write "Age"
      
            Input Age
      
            Write "Location"
             
            Iput Location

      }

Call Avg_Age sub program
Call Texas_ sub program

Output "AvgAge"
Output "Texas"

End

Avg_age Sub program

Declare Sum as int
Delclare avg as float
set sum to = 0
 For (i=0, i< 100, i ++)

     sum = sum + Age[i ]
 AvgAge = sum/10

End Sub Program

Subprogram Texas_

For (i=0, i< 100, i ++)
  If Location == Texas
 Then output Name[i ]
End if

End sub program


     
0
 
TommySzalapskiCommented:
In real code, you would need to put quotes around "Texas" for the if.

Also, for the average, shouldn't you use /100 not /10 ?

For the input function, you might want to specify that the data is going in the arrays. Like
            Write "Name"
           
            Input Name[i ]
0
 
TommySzalapskiCommented:
Other then that it looks good.
0
 
bcg_81Author Commented:
Yes, /100.....In the main module where is says Output "Texas"  this is wrong correct? Thanks!
0
 
TommySzalapskiCommented:
I meant your if line in the for loop should look like
if Location[i ] == "Texas"

Although I don't get those output lines anyway
Why Output "Texas" ? You already output all the names.
0
 
bcg_81Author Commented:
I appreciate all comments but I found the most help for my question from tommy.  Thank you very much.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 12
  • 7
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now