Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
the question is to write a function in to take an array and check if it's sorted

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>
int check(int n,int a[]);
int main()
{
    int a[100],n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
    printf("%d",a[n]);
    check(int n,int a[]);
    if (check(int n,int a[]) = 1)
        {
            printf("the array isn't sorted");
        }`
    return 0;
}
int check(int n,int a[])
{
  int i,s;
  for(i=0;i<n;i++)
    {
        if(a[i] < a[i+1])
        {
            s = a[i];
            a[i] = a[i+1];
            a[i+1] = s;
            return 0;

    }
        else {
            return 1;
        }
    }
}
Posted
Updated 7-Apr-17 12:40pm
v3
Comments
[no name] 7-Apr-17 9:52am    
Tell your teacher that he needs to teach his classes how to use the debugger to debug their homework assignments.
nv3 7-Apr-17 14:46pm    
How true! My virtual 10.

There are multiple problems. Some of them should be already indicated by the compiler.

See the comments:
int main()
{
    int a[100],n,i;
    /* Hopefully the user know what to enter without guiding him. */
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    /* The element a[n] is not initialised: Garbage will be printed out */
    printf("%d",a[n]);
    /* C basics: How to call a function. The compiler should complain about this */
    check(int n,int a[]);
    /* As above.
       Using a value of 1 as not sorted is rather uncommon. 
    */
    if (check(int n,int a[]) = 1)
    {
        printf("the array isn't sorted");
    }
    return 0;
}

int check(int n,int a[])
{
    int i,s;
    for(i=0;i<n;i++)
    {
        /* Whith the last loop iteration (i == n-1) 
           this will access again the unitialised element a[n].
            So the loop should start at 1 when comparing with the previous item 
            or the end condition should be < n-1.
        */
        if(a[i] < a[i+1])
        {
            s = a[i];
            a[i] = a[i+1];
            a[i+1] = s;
            return 0;
        }
        else {
            return 1;
        }
    }
}

The requirement is to check if the array is sorted but the above code will modify one element and return zero when any element is less than the next. From my point of view the array should not be modified. Also I would think of being sorted means that all elements are in ascending order. So the condition to leave the loop and return "not sorted" would be when any element is larger than the next (or smaller than the previous).
 
Share this answer
 
Look at your code: I'll remove the irrelevent-to-your-problem bits.
int check(int n,int a[])
    {
    for(i=0;i<n;i++)
        {
        if(a[i] < a[i+1])
            {
            return 0;
            }
        else 
            {
            return 1;
            }
        }
    }
So, if n is greater than 1, how does i ever get to be 2?

When you fix that, it will fail, probably with a segmentation error because you are looking at one more element than the input array holds:
if(a[i] < a[i+1])
But i ranges from 0 to n - 1, so i+1 is from 1 to n while your array indexes run from 0 to n - 1
 
Share this answer
 
You are asked to check if an array is sorted or not, you are not asked to sort the array, so why are you swapping items in the array ?
C++
s = a[i];
a[i] = a[i+1];
a[i+1] = s;

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900