# Difficulty Retrieving from Arrays

Hello,
I’m a newbie here. I have been trying to figure how to write a code for calculating the sensitivity (% change in resistance) between resistor values. I am using an array of just 2 elements representing the resistor values, which I would input manually and have the code compute the sensitivity which is S = ((R2-R1)/R1)*100. But the “input Resistor value” keeps repeating more than 2 times as specified in the code and doesn’t calculate the sensitivity.
Here’s my code below:

1 float R[2];
2 int [/color] R2Values;
3 int i;
4 float s = ((R[1]-R[0])/R[0])100
5
6 void setup() {
7 // put your setup code here, to run once:
8 Serial.begin(9600);
9 }
10 void loop() {
11 // put your main code here, to run repeatedly:
12 Serial.println("How many resistor values? ");
13 while (Serial.available()==0){
14
15 }
16 R2Values=Serial.parseInt();
17 for (i=0; i<R2Values; i+1){
18 Serial.println(“Input Resistor value” );
19 while(Serial.available()==0){
20
22 }
23 R
=Serial.parseFloat();*
24 }
25 for[/color] (i=0; i<R2Values; i+1){
26 Serial.println("Sensitivity is ");
27 Serial.print(s)

I've deleted your other cross-post @Tesla_tamer.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a suspension from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

``````void loop() {
11  // put your main code here, to run repeatedly:
12  Serial.println("How many resistor values? ");
13  while (Serial.available()==0){
14
15  }
16  R2Values=Serial.parseInt();
17  for (i=0; i<R2Values; i+1){
18    Serial.println("Input Resistor value" );
19    while(Serial.available()==0){
20
22    }
23    R=Serial.parseFloat();
24  }
25   for[/color] (i=0; i<R2Values; i+1){
26    Serial.println("Sensitivity is ");
27    Serial.print(s)
``````

The issue you are having, as you stated is that this code

``````or (i=0; i<R2Values; i+1){
18    Serial.println("Input Resistor value" );
19    while(Serial.available()==0){
``````

runs lots and lots of times but you want it to only print A2X times.

Are you aware what this does `void loop() ` to all the code located in its domain?

If you want to only print the results 9w2 times then you need to write code that will handle loop() running more then 2 times.

Also, this code

``````for (i=0; i<R2Values; i+1){
...
``````

never increments the variable ‘i’ but only add 1 to it and then throws the result away.
you want

``````for (i=0; i<R2Values; i=i+1){
...
``````

for the more traditional C/C++ way

``````or (i=0; i<R2Values; i++){
...
``````
``````float R[2];
int R2Values;

// THIS DOES NOT DEFINE A FUNCTION.  It only initializes a global variable:
//                 float s = ((R[1] - R[0]) / R[0]) * 100
// To get a NEW value for 's' when you change R[0] or R[1] you can make 's' a function
float s()
{
return ((R[1] - R[0]) / R[0]) * 100;
}

void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop()
{
// put your main code here, to run repeatedly:
while (Serial.available()) Serial.read();  // Clear the input buffer
Serial.println("How many resistor values? ");
while (Serial.available() == 0);  // Wait for input
R2Values = Serial.parseInt();

for (int i = 0; i < R2Values; i++)
{
while (Serial.available()) Serial.read();  // Clear the input buffer
Serial.println("Input Resistor value" );
while (Serial.available() == 0);  // Wait for input
R[i] = Serial.parseFloat();
}

for (int i = 0; i < R2Values; i++)
{
Serial.println("Sensitivity is ");
Serial.print(s());
}
}
``````