Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 8th, 2005, 11:53 PM   #1
bjbcartmen
Newbie
 
Join Date: Oct 2005
Posts: 8
Rep Power: 0 bjbcartmen is on a distinguished road
need help passing array values

i need to finish this program by friday at 8:00am i know im new but i have a lot of work done so pretty much here is what i have.

requirements

programmer defined function for into - check
user must input element statistics into element_t im pretty sure i made that
programmer defined function for scan_element to get element stats and store them into element_t
print function that prints the heaviest element

now it all worked until i broke it up into pdf's so i think i jsut had issues with passing data, but im burned out and cant see in my code anymore so im asking for help.


#include <stdio.h>                    //got your standard input output header here
typedef struct element_t                //defines element as a data structure
{
int atomic_num; char name[20]; char symbol[3]; char class[20]; float weight;
} element_t;                               //sets new type called elem
 
 
void intro_display (void);            //intro message for end user  
 
void scan_element ();       //programmer defined function for  scan_element
 
 
void print_element (element_t *,int);       // programmer defined function for print_element			
 
 
int main(void)                        //this is the start of the main function
{
element_t elem[4];
 
intro_display();
 
scan_element ();
 
print_element(elem, 4);            //calls PDF power_elem
 
return 0;                    
}
 
 
 
void scan_element ()
{
element_t elem [4];                 //initializes 4 element types
 
int i=0;                             // i is the variable used as a control for loops
 
for (i=0; i<4; i++)                  //loops input so that four elements may be entered in the data type elem
{
 
  printf("\n What is element number %d's atomic number? ",i+1);		//prompts for the atomic number to be entered.
 
  scanf("%d", &elem[i].atomic_num);	 //inputs user data to element's atomic number portion of the structure
 
  printf("\n What is element number %d's full name? ",i+1);		//prompts for the element number to be entered.
 
  scanf("%s", &elem[i].name); 	// inputs the name of the element into the name portion of the elem structure
 
  printf("\n What is element %d's symbol? ",i+1);			//prompts the user to input the symbol or the element
 
  scanf("%s", &elem[i].symbol);	//inputs the symbol of the element into the name portion of the elem structure
 
  printf("\n What is element %d's class? ",i+1);			//prompts the end user to input the elelment's class.
 
  scanf("%s", &elem[i].class);	//inputs the elements class type
 
  printf("\n What is element %d's atomic weight? ",i+1);		//prompts the user to input the elelments atomic weight
 
  scanf("%f/n/n", &elem[i].weight); //inputs the elements atomic weight into the weight portion of structure elem
 
 }
}
 
void intro_display()
{
  printf("Welcome to Bryant's Official Atomic Weight Tabulator");
}
 
 
 
void print_element( element_t *chem_ptr,int size)
{
printf("\n    Atomic number | Name | Symbol | Class | Atomic wieght");	//a table top that breaks the file into categories
 
if(chem_ptr[0].atomic_num > chem_ptr[1].atomic_num)
{
if(chem_ptr[0].atomic_num > chem_ptr[2].atomic_num)
{
if(chem_ptr[0].atomic_num > chem_ptr[3].atomic_num) 	// simple sort function useed to determine if element 0 is largest
{
printf("\n%d		%s	%s	%s		%4.2f \n\n", chem_ptr[0].atomic_num, chem_ptr[0].name, 
chem_ptr[0].symbol,chem_ptr[0].class, chem_ptr[0].weight);	// if the condition is met its stats are printed
}
}
}
 
else if(chem_ptr[1].atomic_num > chem_ptr[2].atomic_num)
{
if(chem_ptr[1].atomic_num > chem_ptr[3].atomic_num)		// because chem_ptr[0] has been eliminated the test is even smaller but that same concept applies
{
printf("\n%d		%s	%s	%s		%4.2f \n\n", chem_ptr[1].atomic_num, chem_ptr[1].name, 
chem_ptr[1].symbol,chem_ptr[1].class, chem_ptr[1].weight);	// if the above conditions are met the results are printed
}
}
 
else if(chem_ptr[2].atomic_num > chem_ptr[3].atomic_num)	// using the logic above the if statement becomes even smaller
{
printf("\n%d		%s	%s	%s	%4.2f \n\n", chem_ptr[2].atomic_num, chem_ptr[2].name, 
chem_ptr[2].symbol,chem_ptr[2].class, chem_ptr[2].weight);	// also if the condition is met it is printed to the screen
}
 
  else if(chem_ptr[3].atomic_num > chem_ptr[2].atomic_num)	// using the logic above the if statement is at its final stage
  {
   printf("\n%d		%s	%s	%s	%4.2f \n\n", chem_ptr[3].atomic_num, chem_ptr[3].name, 
chem_ptr[3].symbol,chem_ptr[3].class, 
chem_ptr[3].weight);						// if the conditions above were not met this is displayed to the end user.
  }
 
return;
}
bjbcartmen is offline   Reply With Quote
Old Dec 9th, 2005, 12:20 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 876
Rep Power: 4 The Dark is on a distinguished road
You are almost there I think.
1. Change your scan_element to take the array and size as a variable - do this in the same way that print_element is done
void scan_element( element_t *chem_ptr,int size)

2. You don't need a lot of if statements in print_element to determine the heaviest element - a loop would be much easier
int heaviestElementNo = 0;
int i;
for (i=1; i<size; i++)
{
  if(chem_ptr[i].atomic_num > chem_ptr[heaviestElementNo].atomic_num)
   heaviestElementNo = i;
}
printf("\n%d		%s	%s	%s		%4.2f \n\n", chem_ptr[heaviestElementNo].atomic_num, chem_ptr[heaviestElementNo].name, 
chem_ptr[heaviestElementNo].symbol,chem_ptr[heaviestElementNo].class, chem_ptr[heaviestElementNo].weight);

3. Are you sure you meant /n/n in that last scanf?
The Dark is offline   Reply With Quote
Old Dec 9th, 2005, 1:25 AM   #3
bjbcartmen
Newbie
 
Join Date: Oct 2005
Posts: 8
Rep Power: 0 bjbcartmen is on a distinguished road
ok well its still messing up. I put in all the element stats for random things and i get garbled results back, anymore help would be appreciated.

heres my current code.

#include <stdio.h>                    //got your standard input output header here
typedef struct element_t                //defines element as a data structure
{
int atomic_num; char name[20]; char symbol[3]; char class[20]; float weight;
} element_t;                               //sets new type called element_t


void intro_display (void);            //intro message for end user  

void scan_element ();       //programmer defined function for  scan_element


void print_element (element_t*,int);       // programmer defined function for print_element			


int main(void)                        //this is the start of the main function
{
element_t elem[4];

intro_display();

scan_element ();

print_element(elem, 4);            //calls PDF power_elem

return 0;                    
}



void scan_element( element_t *chem_ptr,int size)
{
element_t elem [4];                 //initializes 4 element types

int i=0;                             // i is the variable used as a control for loops

for (i=0; i<4; i++)                  //loops input so that four elements may be entered in the data type elem
{

  printf("\n What is element number %d's atomic number? ",i+1);		//prompts for the atomic number to be entered.

  scanf("%d", &elem[i].atomic_num);	 //inputs user data to element's atomic number portion of the structure

  printf("\n What is element number %d's full name? ",i+1);		//prompts for the element number to be entered.

  scanf("%s", &elem[i].name); 	// inputs the name of the element into the name portion of the elem structure

  printf("\n What is element %d's symbol? ",i+1);			//prompts the user to input the symbol or the element

  scanf("%s", &elem[i].symbol);	//inputs the symbol of the element into the name portion of the elem structure

  printf("\n What is element %d's class? ",i+1);			//prompts the end user to input the elelment's class.

  scanf("%s", &elem[i].class);	//inputs the elements class type

  printf("\n What is element %d's atomic weight? ",i+1);		//prompts the user to input the elelments atomic weight

  scanf("%f/n/n", &elem[i].weight); //inputs the elements atomic weight into the weight portion of structure elem

 }
}

void intro_display()
{
  printf("Welcome to Bryant's Official Atomic Weight Tabulator");
}



void print_element( element_t *chem_ptr,int size)
{
int heaviestElementNo = 0;
int i;
for (i=1; i<size; i++)
{
  if(chem_ptr[i].atomic_num > chem_ptr[heaviestElementNo].atomic_num)
   heaviestElementNo = i;
}
printf("\n%d		%s	%s	%s		%4.2f \n", chem_ptr[heaviestElementNo].atomic_num, chem_ptr[heaviestElementNo].name, 
chem_ptr[heaviestElementNo].symbol,chem_ptr[heaviestElementNo].class, chem_ptr[heaviestElementNo].weight);
return;
}
bjbcartmen is offline   Reply With Quote
Old Dec 9th, 2005, 5:55 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by The Dark
1. Change your scan_element to take the array and size as a variable - do this in the same way that print_element is done
void scan_element( element_t *chem_ptr,int size)
Your code should now look like:
#include <stdio.h>

typedef struct element_t		//defines element as a data structure
{
	int atomic_num;
	char name[20];
	char symbol[3];
	char class[20];
	float weight;
} element_t;		//sets new type called element_t


void intro_display (void);				//intro message for end user  
void scan_element (element_t*, int);		//programmer defined function for  scan_element
void print_element (element_t*, int);	// programmer defined function for print_element			


int main(void)                        //this is the start of the main function
{
	element_t elem[4];
	intro_display();
	scan_element(elem, 4);
	print_element(elem, 4);            //calls PDF power_elem
	return 0;                    
}

void scan_element( element_t *chem_ptr, int size)
{
	int i=0;
	//loops input so that four elements may be entered in the data type elem
	for (i=0; i<size; i++)
	{
		//prompts for the atomic number to be entered.
		printf("\n\nWhat is element number %d's atomic number? ",i+1);
		scanf("%d", &chem_ptr[i].atomic_num);
		
		//prompts for the element number to be entered.
		printf("\nWhat is element number %d's full name? ",i+1);
		scanf("%s", &chem_ptr[i].name);
		
		//prompts the user to input the symbol or the element
		printf("\nWhat is element %d's symbol? ",i+1);
		scanf("%s", &chem_ptr[i].symbol);
		
		//prompts the end user to input the elelment's class.
		printf("\nWhat is element %d's class? ",i+1);	
		scanf("%s", &chem_ptr[i].class);
		
		//prompts the user to input the elelments atomic weight
		printf("\nWhat is element %d's atomic weight? ",i+1);
		scanf("%f", &chem_ptr[i].weight);
	}
}

void intro_display()
{
	printf("Welcome to Bryant's Official Atomic Weight Tabulator\n");
}



void print_element( element_t *chem_ptr, int size)
{
	int HEnum = 0;
	int i;
	for (i = 0; i < size; i++)
	{
		if(chem_ptr[i].atomic_num > chem_ptr[HEnum].atomic_num)
			HEnum = i;
	}
	printf("\nHeavies Element is: %d, %s, %s, %s, %4.2f\n\n", chem_ptr[HEnum].atomic_num, chem_ptr[HEnum].name, chem_ptr[HEnum].symbol,chem_ptr[HEnum].class, chem_ptr[HEnum].weight);
	return;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Dec 19th, 2005, 5:54 AM   #5
bjbcartmen
Newbie
 
Join Date: Oct 2005
Posts: 8
Rep Power: 0 bjbcartmen is on a distinguished road
ok its almost there im am having problems with the last part though, where i do symbol, it always comes out as the wrong value and i dont know why i have triple checked and just cant find the error

 
/*PROGRAM NAME: Bryant Benjamin's Atomic Weight Calculator 

PROGRAMMER:  Bryant Benjamin

PURPOSE:  To Complete Homework Problem number 12 posted on the website, which is  a modified version of a problem on page 602.

DESCRIPTION:  What happens is the user inputs four elements. The program using its awesome ability to store user defined data types will the n sort those elements based on their atomic
weight and then output the full stats of the heaviest element.

ALGORITHM IN PSEUDOCODE:
step one:

A greeting will be displayed to the user.

step two:
A data type will be made to store the features of the elements entered.

step three:
Then the user will enter the charecteristics of four separate elements

step 4:
The computer using its extensive knowlege of greater then and less then will then figure out which element's atomic mass is greatest.

step 5:
The user will then see the charecteristics of the heaviest element. 

Date Submitted: December 8th, 2005

Known bugs: none
*/



#include <stdio.h>

typedef struct element_t		//defines element as a data structure
{
	int atomic_num;
	char name[20];
	char symbol[3];
	char class[20];
	float weight;
} element_t;		//sets new type called element_t


void intro_display (void);				//intro message for end user  
void scan_element (element_t*, int);		//programmer defined function for  scan_element
void print_element (element_t*, int);	// programmer defined function for print_element			


int main(void)                        //this is the start of the main function
{
	element_t elem[4];
	intro_display();
	scan_element(elem, 4);
	print_element(elem, 4);            //calls PDF power_elem
	return 0;                    
}

void scan_element( element_t *chem_ptr, int size)
{
	int i=0;
	//loops input so that four elements may be entered in the data type elem
	for (i=0; i<size; i++)
	{
		//prompts for the atomic number to be entered.
		printf("\n\nWhat is element number %d's atomic number? ",i+1);
		scanf("%d", &chem_ptr[i].atomic_num);
		
		//prompts for the element number to be entered.
		printf("\nWhat is element number %d's full name? ",i+1);
		scanf("%s", &chem_ptr[i].name);
		
		//prompts the user to input the symbol or the element
		printf("\nWhat is element %d's symbol? ",i+1);
		scanf("%s", &chem_ptr[i].symbol);
		
		//prompts the end user to input the elelment's class.
		printf("\nWhat is element %d's class? ",i+1);	
		scanf("%s", &chem_ptr[i].class);
		
		//prompts the user to input the elelments atomic weight
		printf("\nWhat is element %d's atomic weight? ",i+1);
		scanf("%f", &chem_ptr[i].weight);
	}
}

void intro_display()
{
	printf("Welcome to Bryant's Official Atomic Weight Tabulator\n");
}



void print_element( element_t *chem_ptr, int size)
{
	int HEnum = 0;
	int i;
	for (i = 0; i < size; i++)
	{
		if(chem_ptr[i].atomic_num > chem_ptr[HEnum].atomic_num)
			HEnum = i;
	}
	printf("\nHeavies Element is: Atomic number: %d, Name: %s, Symbol: %s, Class: %s, Weight: %4.2f\n\n", 
chem_ptr[HEnum].atomic_num, chem_ptr[HEnum].name, chem_ptr[HEnum].symbol, chem_ptr[HEnum].class, chem_ptr[HEnum].weight);

	return;
}
bjbcartmen is offline   Reply With Quote
Old Dec 19th, 2005, 6:46 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
works for me.
Quote:
Originally Posted by output
Welcome to Bryant's Official Atomic Weight Tabulator


What is element number 1's atomic number? 12

What is element number 1's full name? j

What is element 1's symbol? jj

What is element 1's class? jjj

What is element 1's atomic weight? 10


What is element number 2's atomic number? 14

What is element number 2's full name? k

What is element 2's symbol? kk

What is element 2's class? kkk

What is element 2's atomic weight? 20


What is element number 3's atomic number? 8

What is element number 3's full name? l

What is element 3's symbol? ll

What is element 3's class? lll

What is element 3's atomic weight? 30


What is element number 4's atomic number? 7

What is element number 4's full name? u

What is element 4's symbol? uu

What is element 4's class? uuu

What is element 4's atomic weight? 78

Heavies Element is: Atomic number: 14, Name: k, Symbol: kk, Class: kkk, Weight:20.00
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Dec 19th, 2005, 3:51 PM   #7
bjbcartmen
Newbie
 
Join Date: Oct 2005
Posts: 8
Rep Power: 0 bjbcartmen is on a distinguished road
look your heaviest element isnt even the heaviest this program is seriosuly messed up
bjbcartmen is offline   Reply With Quote
Old Dec 19th, 2005, 5:16 PM   #8
spydoor
Programmer
 
Join Date: Feb 2005
Posts: 64
Rep Power: 4 spydoor is on a distinguished road
{
	if(chem_ptr[i].atomic_num > chem_ptr[HEnum].atomic_num)
		HEnum = i;
}

how about weight?
spydoor is offline   Reply With Quote
Old Dec 20th, 2005, 4:54 AM   #9
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by bjbcartmen
look your heaviest element isnt even the heaviest this program is seriosuly messed up
that's because you're sorting by atomic number and not atomic weight.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Dec 20th, 2005, 11:08 AM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by bjbcartmen
look your heaviest element isnt even the heaviest this program is seriosuly messed up
How simple to solve can it get?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC