Problem 1. Electrical resistance in various metals can be measured using nano-ohm metres

(n

m). Table 1 gives the resistivity of several metals.

Material Resistivity (n

m)

Copper 16.78

Aluminum 26.50

Beryllium 35.6

Potassium 72.0

Iron 96.10

Table 1: Resistivity of several metals

Write a program that prompts the user for an observed resistivity of an unknown material

(as nano-ohm metres) and identies the substance if the observed resistivity is within

3% of the known resistivity of any of the materials in Table 1. If the input value lies

outside the 3% range, output Unknown substance.

code:

``#include<stdio.h>int main(int argc, char **argv){int substance;printf("What is the resistivity of your substance in nano-ohm metres:\n");scanf("%d", &substance);if (substance <=(16.78*1.03) && substance>= (16.78*0.97)){printf("Your substance is copper.");}else if (substance <=(26.5*1.03) && substance>= (26.5*0.97)){printf("Your substance is aluminum.");}else if (substance <=(35.6*1.03) && substance>= (35.6*0.97)){printf("Your substance is beryllium.");}else if (substance <=(72*1.03) && substance>= (72*0.97)){printf("Your substance is potassium.");}else if (substance <=(96.1*1.03) && substance>= (96.1*0.97)){printf("Your substance is iron.");}else{printf("Unknown substance");}getchar();getchar();return 0;}``

Mmmmm... %d in your scanf() isn't going to be a very good format for reading real numbers... or floats which is what your resistivities seem to be.

Also, how about an array with element names and corresponding resistivities and a loop that looks through the resistivities in the array rather than some very confusing, very nested series of if statements?

The problem is that you're comparing a int value with double values. Also you're reading integer values.

Try declaring

``````    double substance;
``````

``````     scanf("%lf", &substance);