Skip to content

A simplish C program to demonstrate use of array

February 19, 2014

This program took me about 90 minutes to complete.  I only put that time, because I am using a little slower than that, but not dealing with any graphic interfaces, classes or other object orientation rules, I found it quicker to accomplish actually building the program.  As always, I demonstrate good structured programming techniques with use of functions and a top down approach.

This program will ask the user a series of questions about their health and give a simple synopsis on their longevity.  Very simple, but enough to demonstrate how a more complex program like this could look.

#include <stdio.h>
#include <stdlib.h>

//function prototypes
void display_heading(void);
int getData(int);
void print_array(int scores[]);
void determine_outcome(int scores[]);

int main(void){

display_heading();

int i = 1;

puts(“\n\nWant to see how healthy you are?  Enter a 1 for yes, 0 to Quit”);
scanf(“%d”,&i);

int counter;
int scores[4];

if(i==1){// play
for(counter=0;counter<5;counter++){
scores[counter] = getData(counter); //put response in the scores array
}// end for

print_array(scores);//just for debugging purposes really
determine_outcome(scores);//will summize responses and show a response

}else{
puts(“\n\n\t\t Remember to choose 1 to play when asked!!”);
}//end if

}//end main

void display_heading(){
puts(“\t\t\tThe life calculator\n\\n”);
puts(“Follow these simple life rules and you will live long and prosper\n\n”);
puts(“1 – Eat well\n”);
puts(“2 – get lots of rest\n”);
puts(“3 – exercise on a regular basis\n”);
puts(“4 – reduce stress in your life\n”);
puts(“5 – relax, relax, relax”);
}

int getData(int i){
int value;

if(i == 0){
puts(“On a scale of 1-5 how healthy do you eat? 1 = real healthy, 5 = not healthy”);
}else if(i == 1){
puts(“On a scale of 1-5 how much rest do you get each day? 1 = lots, 5 = not much”);
}else if(i == 2){
puts(“On a scale of 1-5 how much exercise do you get each day? 1 = a lot, 5 = not much”);
}else if(i == 3){
puts(“On a scale of 1-5 how much stress is in your life? 1 = not much, 5 = lots”);
}
else{
puts(“On a scale of 1-5 how much time do you spend each day relaxing? 1 = lots, 5 = not much”);
}//end if

scanf(“%d”,&value);
return value;

}

void print_array(int scores[]){
int i;
puts(“\n\nScores in the array are: “);
for(i=0;i<5;i++){
printf(“%d”,scores[i],”\t”);
}
system(“pause”);
}

void determine_outcome(int scores[]){
int i,total=0;

for(i=0;i<5;i++){
total = total + scores[i];//total scores
}

if(total < 10){
puts(“You shall live long and prosper my friend!”);
}else if(total >10 && total <15){
puts(“doing well my friend a good long life”);
}else if(total >15 && total < 20){
puts(“Well, you could be doing better, think about some changes soon.”);
}else{
puts(“Your longevity is in serious question!”);
}

system(“pause”);
}

Advertisements

From → C programming

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: