I could have stopped then and there - a happy man. But, I have a tendency to get myself in troubling situations. Somehow, I ended up deciding to write a program to print the electronic configuration of an element given its atomic number. After fighting ferociously with logical and syntactic errors, I managed to complete the program. However, I did not have the patience to handle the exceptions in 3d,4d,5d.
For example, if you enter the atomic number as 15(phosphorus), then the program prints out:
1s2 2s2 2p6 3s2 3p3
Someday, if i gather enough patience, I will modify the program to handle these exceptions. Until then, just look upon the program as a mental exercise.
#include<stdio.h>
#include<stdlib.h>
#define AMIN 1
#define AMAX 120
int get_sub_shell_name(int snumber)
{
if(snumber<1) {
printf("get_sub_shell_name: Invalid shell number %d", snumber);
return -1;
}
switch(snumber) {
case 1: return 's';
case 2: return 'p';
case 3: return 'd';
case 4: return 'f';
default: return 'f'+(snumber-4);
}
}
int get_max_electrons(int n)
{
int total = 2;
int i;
for(i=1;i<n;i++) {
total+=4;
}
return total;
}
/* returns the number of electrons yet to be accounted for */
int print_next_diagonal(int rem, int row, int col)
{
int i = row;
int j = col;
int snumber, sname, enumber;
while(j>0 && rem>0) {
snumber = i;
sname = get_sub_shell_name(j);
enumber = get_max_electrons(j);
if(rem < enumber)
enumber = rem;
printf("%d", snumber);
printf("%c", sname);
printf("%d ", enumber);
rem -= enumber;
i++;
j--;
}
return rem;
}
void print_shell_configuration(int anumber)
{
int i, j, rem, ret;
i = j = 1;
rem = anumber;
while(rem > 0) {
while(j <= i && rem > 0) {
rem = print_next_diagonal(rem, i, j);
j++;
}
i++;
j = i - 1;
}
}
int main()
{
int anumber;
printf("Enter the atomic number of the element:");
scanf("%d", &anumber);
if(anumber < AMIN) {
printf("Atomic number cannot be zero or negative.\n");
exit(1);
}
if(anumber > AMAX) {
printf("The element does not exist. Perhaps you have found a new one.\n");
exit(1);
}
print_shell_configuration(anumber);
printf("\n");
return 0;
}
No comments:
Post a Comment