0

I'm trying to learn C and I want to make a program that compares the number I type with the numbers in my array. The only problem is that It doesn't actually do that. Even if I type a number that is from that array it shows that the number is not from that array.

    #include <stdio.h>
    void getMark(int findMark, double crswk1[]);
    void changePartMark(double crswk1[], int findMark);

    int main()
    {
    int findMark;
    double crswk1[10]={67, 77, 80, 40};

    getMark(findMark, crswk1);
    changePartMark(crswk1, findMark);
}

void getMark(int findMark, double crswk1[])
{
    printf("Enter the mark you want to change: ");
    scanf("%d", &findMark);

}

void changePartMark(double crswk1[], int findMark)
{
    int i;
        if(findMark == crswk1[i])
            {
            printf("It is equal");
            }
        else
            {
            printf("It is not equal");
            }
}
niku nikuD
  • 23
  • 7

2 Answers2

2

The number you're reading in is never getting back to findMark in your main function.

void getMark(int findMark, double crswk1[])
{
    printf("Enter the mark you want to change: ");
    scanf("%d", &findMark);

}

This function is saving a value in the local parameter findMark. Because all parameters in C are passed by value, changes to this local variable are not reflected in the caller, so findMark in main never changes.

You need to change this function to take the address of an `int

void getMark(int *findMark, double crswk1[])
{
    printf("Enter the mark you want to change: ");
    scanf("%d", findMark);
}

Then you call this function from main like this:

getMark(&findMark, crswk1);

By passing in the address of findMark, the function can write to that address.

Also, your changePartMark function doesn't search through the entire array. It only looks at index i. But even that is a problem because you never set i.

You need to loop through the array to check your value against each element in the array.

int i;
for (i=0; i<4 i++) {
    if(findMark == crswk1[i])
        {
        printf("It is equal");
        }
    else
        {
        printf("It is not equal");
        }
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • And what if I just want it to show It is equal, without showing It is not equal for every number that is not the number I want – niku nikuD Nov 20 '17 at 14:34
  • @nikunikuD Have another variable that flags whether or not you found a match, initializing it to false. If you find a match, set the flag to true, otherwise do nothing. When the loop is done, check the value of the flag. – dbush Nov 20 '17 at 14:37
1

Two main issues:

First, the number you enter is never passed back. When you write

void getMark(int findMark, double crswk1[]) {
    printf("Enter the mark you want to change: ");
    scanf("%d", &findMark);
}

then you read in the value in a local copy findMark, not in the one used by the caller. BTW: crswk1 is not used; So I'd suggest to write

int getMark() {
    int findMark = 0;
    printf("Enter the mark you want to change: ");
    scanf("%d", &findMark);
    return findMark;
}

Second, your void changePartMark(double crswk1[], int findMark) lacks a loop, and i is not initialized. Code could look like the following:

void changePartMark(double crswk1[], int findMark)
{
    for (int i=0; i<4; i++) {
        if(findMark == crswk1[i])
            {
            printf("It is equal");
            }
        else
            {
            printf("It is not equal");
            }
    }
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58