1

Observe the following code. I want to divide the amount by the value of the last element in the array. I have tried in the following way but it is not working. Can anyone tell me what is the proper way to do it?

#include<stdio.h>
int main()
{
    int i, j, k, noteNumber, array[100], amount, result;

    printf("Enter the number of the notes: \n");
    scanf("%d", &noteNumber);

    printf("Enter the value of %d notes: \n", noteNumber);
    for(i = 0; i < noteNumber; i++){
        scanf("%d", &array[i]);
    }

    printf("Enter the amount: \n");
    scanf("%d", &amount);

    i = j;

        if(amount / array[j] == 0){
        printf("Minimum %d number of is needed", (amount/array[j]));
        printf("The value of each note is %d", array[j]);
    }

}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • `amount/array[noteNumber-1]` – George Jan 25 '17 at 17:49
  • Define "it is not working". Do you get different output than you expect? If so, then the input you provide, the output you expect, and the output you actually get are appropos additions to your question. – John Bollinger Jan 25 '17 at 17:50
  • last element will be array[noteNumber - 1 ] . It would be helpful if you define the problem more clearl and tell us what is " not working " in your solution. – GAURANG VYAS Jan 25 '17 at 17:56
  • Your compiler was very probably nice enough to greet you with a warning on the `i = j` line - Why did you ignore that? – tofro Jan 25 '17 at 18:17
  • i am new to programming. I did not understand first. But now i do. – Rubel Hosen Jan 25 '17 at 18:23

3 Answers3

3

As I can see

  i = j;

is wrong as you're using the value of an uninitialized variable to assign to another. This does not make any sense and can lead to undefined behavior.

C arrays use 0-based indexing, so for an array of size n, the last index would be n-1.

That said, never use an unbound index for a statically defined array, always perform the bound checking before using the index.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

If noteNumber is the size of the array, then the last Element will be

array[noteNumber - 1]

As far as I can see, j isn't even initialized?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Richard
  • 457
  • 1
  • 6
  • 21
1

You are having a line

 i = j;

j is not even initialized so you are doing a mistake here , maybe what you wanted was

 j = i - 1   

As i would have incremented to noteNumber in your for loop and array with number of elements n has last element index n-1 because index starts from 0 rather than 1.

So Proper Code Would Be

#include<stdio.h>
int main(){

int i, j, k, noteNumber, array[100], amount, result;

printf("Enter the number of the notes: \n");
scanf("%d", &noteNumber);

printf("Enter the value of %d notes: \n", noteNumber);
for(i = 0; i < noteNumber; i++){
    scanf("%d", &array[i]);
}

printf("Enter the amount: \n");
scanf("%d", &amount);

j = i - 1; // Line Changed

    if(amount / array[j] == 0){
    printf("Minimum %d number of is needed", (amount/array[j]));
    printf("The value of each note is %d", array[j]);
}

}
Suraj Jain
  • 4,463
  • 28
  • 39
  • @RubelHosen Thanks A lot , i was just a beginner like you , and i still am , so i understand various problem we go through at starting. Please Read The Book C programming a modern approach 2 edition, it is very nice book or you could check http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Suraj Jain Jan 25 '17 at 18:18
  • @RubelHosen And You can also watch these [videos](http://nptel.ac.in/courses/106104128/) for C understanding C Language. – Suraj Jain Jan 25 '17 at 18:20