Link to problem in question: http://www.cs.montana.edu/~marissa.joehler/csci-112
ID: 3691986 • Letter: L
Question
Link to problem in question:
http://www.cs.montana.edu/~marissa.joehler/csci-112/lab9.html
Lab 7 which is needed in part to complete this.
include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_ELEMENTS 20
#define MAX_LENGTH 100
struct elementT{//form struct
int num;
char name[MAX_LENGTH];
char symbol[MAX_LENGTH];
char class [MAX_LENGTH];
double weight;
int electrons[7];
};
struct elementT Scan();
void Print(struct elemementT e);
int main(int argc, char *argv[]){
if(argc!=2){//only one arg
printf("ERROR: You must provide exactly one arguement to this program. ");
return 0;
}
int count=(int)strol(argv[1], NULL, 10);
if(count>MAX_ELEMENTS || count <=0){
printf("ERROR: You must provide an integer greater than 0 and less than or equal to 20.");
}
struct elementT list[count];//list of elements
int i=0;
for(i=0; i<count; i++){//scan in each element
list[i]=Scan();
}
printf("%d total elements ", count);
int lowest=0;
int highest=0;
for(i=0; i<count; i++){//find both the highest ad the lowest atomic numbers
if(list[i].num>list[highest].num){
highest=i;
}
if(list[i].num<list[lowest0.num){
lowest=i;
}
}
printf("%s had the smallest atomic number ", list[lowest].name);
printf("%s had the largest atomic number ", list [highest].name);
for(i=0; i<count; i++){
Print(list[i]);//print out the elements
}
return 0;
}
struct elementT Scan(){
struct elementT e; //get each part of the element
scanf("%d %s %s %s %lf %d %d %d %d %d %d %d", &e.electrons[4], &e.electrons[5], &e.electrons[6]);
return e;
}
void Print(struct elementT e)
{//printing elements
printf("--------------- | %d %.4lf | %s %s | ", e.num, e.weight, e.symbol, e.name);
int i=0;
for(i=0; i<7; i++){
if(e.electrons[i] !=0){//do not print 0's
printf("%d ", e.electrons[i]);
}
}
printf(" --------------- ");
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_ELEMENTS 20
#define MAX_LENGTH 100
void Print(struct elementT e);
struct elementT{//form struct
int num;
char name[MAX_LENGTH];
char symbol[MAX_LENGTH];
char class1 [MAX_LENGTH];
double weight;
int electrons[7];
};
struct elementT Scan();
void Print(struct elemementT e);
int main(int argc, char *argv[]){
if(argc!=2){//only one arg
printf("ERROR: You must provide exactly one arguement to this program. ");
return 0;
}
int count=(int) strtol(argv[1], NULL, 10);
if(count>MAX_ELEMENTS || count <=0){
printf("ERROR: You must provide an integer greater than 0 and less than or equal to 20.");
}
struct elementT list[count];//list of elements
int i=0;
for(i=0; i<count; i++){//scan in each element
list[i]=Scan();
}
printf("%d total elements ", count);
int lowest=0;
int highest=0;
for(i=0; i<count; i++){//find both the highest ad the lowest atomic numbers
if(list[i].num>list[highest].num){
highest=i;
}
if(list[i].num<list[lowest].num){
lowest=i;
}
}
printf("%s had the smallest atomic number ", list[lowest].name);
printf("%s had the largest atomic number ", list [highest].name);
for(i=0; i<count; i++){
Print(list[i]);//print out the elements
}
return 0;
}
struct elementT Scan(){
struct elementT e; //get each part of the element
scanf("%d %s %s %s %lf %d %d %d %d %d %d %d", &e.electrons[4], &e.electrons[5], &e.electrons[6]);
return e;
}
void Print(struct elementT e)
{//printing elements
printf("--------------- | %d %.4lf | %s %s | ", e.num, e.weight, e.symbol, e.name);
int i=0;
for(i=0; i<7; i++){
if(e.electrons[i] !=0){//do not print 0's
printf("%d ", e.electrons[i]);
}
}
printf(" --------------- ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.