![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 8
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 876
Rep Power: 4
![]() |
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? |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 8
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#4 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
#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 |
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2005
Posts: 8
Rep Power: 0
![]() |
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;
} |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
works for me.
![]() Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2005
Posts: 8
Rep Power: 0
![]() |
look your heaviest element isnt even the heaviest
this program is seriosuly messed up ![]() |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
{
if(chem_ptr[i].atomic_num > chem_ptr[HEnum].atomic_num)
HEnum = i;
}how about weight? |
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|