This is a part of the question of my assignment and i am facing problems on how to access the array in a function.

Write a function that finds the 2nd minimum value among the given data items of float type, and returns the 2nd minimum. You should use pointer syntaxes over the data items.

//
//  main.c
//  DynamicMemoryManagement
//
//  Created by Ramanpreet Singh on 12-10-20.
//  Copyright (c) 2012 Ramanpreet Singh. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

float* assignMemory(int count);
void readFloatArray(float *f, int count);
float secondSmallest(float *f, int count);

int main(int argc, const char * argv[])
{
    int count;

    printf("Enter count: ");
    scanf("%d", &count);

    float *f = assignMemory(count);

    printf("\nMemory of F: %p", f);

    readFloatArray(f, count);
    int i;
    printf("\nYour Entered array is: ");
    for (i = 0; i < count; i++)
    {
        printf("\n%9.2f", f[i]);
    }

    float secondSmall = secondSmallest(f, count);

    if (secondSmall != 0)
    {
        printf("\nSecond Smallest number in array: %f", secondSmall);
    }

}

/*
 */
float* assignMemory(int count)
{
    float *f = (float*)malloc(count * sizeof(float));
    return f;
}


/*
*/
void readFloatArray(float *f, int count)
{
    printf("\n\nEnter you float values: \n");
    int i;
    for (i = 0; i < count; i++)
    {
        scanf("%f", &f[i]);
    }
}

/*
*/
float secondSmallest(float *f, int count)
{
    float *smallest = (float*) -1;
    float *secondSmall = (float*) -1;
    int i;

    if (count == 1)
    {
        printf("\nYou have only one item in your array,\n"
               "therefore, you have no second smallest item.");
        return 0;
    }

    if(f[1] > f[0])
    {
        *smallest = f[0];   //The if works fine, for two elements
        *secondSmall = f[1];
    }
    else if(f[1] < f[0])
    {
        *smallest = f[1];  // I GET AN ERROR AT THIS LINE - Bad access 
        *secondSmall = f[0];
    }

    for (i = 2; i < count; i++)
    {
        if (f[i] < *smallest)
        {
            *secondSmall = *smallest;
            *smallest = f[i];
        }
        else if(f[i] < *secondSmall)
        {
            *secondSmall = f[i];
        }
    }

    return *secondSmall;
}

So i want to know whatam i doing wrong... Theres no error in the program. But when i run the program and when it reaches the secondSmallest() function and tries to access the array passed through it, The program thorws an error. Any help with this function would be really great, thanks in advance and the help would be appreciated.

Recommended Answers

All 6 Replies

It has nothing to do with the "array" and everything to do with the wacky use of pointers for smallest and secondSmall. Just make those regular float variables, there's no need for pointers there.

If have the get the following output

Enter count: 4

Memory of F: 0x100100a70

Enter you float values: 
32
2
24
25

Your Entered array is: 
    32.00
     2.00
    24.00
    25.00

** --- Does not go beyond this... **
but if the first item is smaller than the second ... Then i get the correct results..
Only when the second item is smaller than the first item i get the above given output.

When it is correct :

Enter count: 4

Memory of F: 0x100100a70

Enter you float values: 
3
4
5
6

Your Entered array is: 
     3.00
     4.00
     5.00
     6.00
Second Smallest number in array: 4.000000

(Editied Post)

Ok i got it why it does not go further than that is because it assigns secondSmallest = 0
But why does it do that??

I would have made them regular pointers but My assignment question asks me to Use pointer expression.. This is my question:

Q.3. Write a function that finds the 2nd minimum value among the given data items of float type, and returns the 2nd minimum. You should use pointer syntaxes over the data items.

Thats just how my instructor wants it. I have no problem in using the float variables.

Is there any problem with my program or is it just pointer in 'C' that have some problem??

I figured it out

else
{
    *secondSmall = f[0];
    *smallest = f[1];
}

Just by flipping the secondSmallest with the smallest, it works fine.
So i guess it is something on how the pointer access the memory. I guess it cannot go back and access the previous memory location for that you might need some additional coding, I guess.

Thats just how my instructor wants it.

You're misunderstanding the question. Your teacher wants you to use pointer arithmetic when accessing the "array" rather than subscripting with an index. For example:

smallest = *(f + i);

Is there any problem with my program

Well, your algorithm is completely wrong for what it's trying to do. I suspect you just jumped in and started writing code without working out all of the potential case examples in a small set on paper. If you don't understand the problem or how to solve it methodically, you can't write a program to do it for you. Here are the cases I'd use. First, the average case permutations:

  1. 1 2 3
  2. 1 3 2
  3. 2 1 3
  4. 2 3 1
  5. 3 1 2
  6. 3 2 1

Then obvious degenerate cases (such as an empty array or no second smallest number):

  1. <empty array>
  2. 1
  3. 1 1
  4. 1 1 1

Once you can lay out detailed steps to accomplish the goal in each of those cases on paper, you can start writing code to do it.

or is it just pointer in 'C' that have some problem??

Pointers in C have no problem, you're just using them incorrectly. Don't blame the language for your poor use of it.

Ok, great i will follow your method. Thanks for your help and yes i will change my code so that it uses pointer arithmetic and yes it should have a check for an empty class and when all the elements have the same value. Your help is appreciated Sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.