Thread: New to C, need help understanding errors

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Ok so I fixed a few things and changed some of the variables that where written as two different things but should have been carried over as one. So JDGATX, your saying to get rid of the variable n?

    here is the new code I think I have get_min_range right now Ill look at sort_arr

    Code:
    // Array.cpp : Defines the entry point for the console application.
    //
    
    #include<stdio.h>
    #include<stdafx.h>
    #include"conio.h"
    #include<math.h>
    
    #define x 10
    
    int get_min_range(int arr[], int small, int last)
    	
    {	
    
    	small=0;
    
    	for(last=1; last<x; ++last)
    		if (last<small)
    			arr[small]=arr[last];
    	
    	return(small);
    }
    
    
    void sort_arr(int arr[], int n)
    {
    
    	int small, temp=0, index_of_min;  
    
        for (small = 0; small < x-1; ++small) 
    		index_of_min = get_min_range(arr, small, x-1);
    
        if (small != index_of_min) 
    		temp = arr[index_of_min];
        arr[index_of_min] = arr[small];
    	arr[small] = temp;
    }
    
    
    
    int main()
    {
    	
    	int f=0, n=0, arr[x];
    
    	
    	for(int n=0; n<x; n++)
    	{
    		printf("Enter a number in the array:");
    		scanf_s("&#37;d", &arr[n]);
    	}
    
    	sort_arr( arr, n);
    
    	for(f=0; f<x; f++)
    		printf("%d", arr[f]);
    	getch();
    	return (0);
    }

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    My problem is that it is only switching the first and last values if they are not in order.

  3. #18
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    There's still no point in passing in 'small' under your current implementation because you just set it to zero immediately upon entering the get_min_range function.

  4. #19
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    You still have the same problem in your sort_arr function that you did the LAST time I commented on it. Since this:

    Code:
    arr[small] = temp;
    ...is outside your for loop, small will always be x-1 when that statement executes, meaning you will always swap the last position, no matter what.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    Thanks guys for the help I got a friend who is good at this he helped me too but thanks for all the suggestions they really helped.

  6. #21
    Registered User
    Join Date
    Apr 2008
    Location
    Sydney, Australia
    Posts
    6
    Hi All,

    3wit, please post the final code, so we all can learn from it. Cheers.

    coolboarderguy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. errors using my parser- MSVC++ 6, bhtypes.h
    By AeroHammer in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2005, 07:11 PM
  2. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  3. opengl Nehe tutorial errors when compiling
    By gell10 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2003, 08:09 PM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM