You are viewing our Forum Archives. To view or take place in current topics click here.
C Programming Help
Posted:

C Programming HelpPosted:

Almac14
  • 2 Million
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
So I just need some help with passing the array in main. I know the code works fine and everything as if you just put it in main it runs but my assignment is to use functions and I also need to figure out how to add another array. Any help is appreciated, Thank You.


#include <stdio.h>
#include<time.h>                  // for time()
#include<stdlib.h>                //for rand()

void sortArray();

int main()
{
    int x;
    srand(time(NULL));                       //random number generator
   
    printf("Enter number of elements\n");
    scanf("%d", &x);
   
    //printf("Enter %d integers\n", n);
   
   
   
    return 0;
}

void sortArray()
{
    int array[100];
    int c, n, d, position, swap;
   
    for ( c = 0 ; c < n ; c++ )
        array[c]=rand()%100;                 // storing a random number between 0 and 100
   
    for ( c = 0 ; c < ( n - 1 ) ; c++ )
    {
        position = c;
       
        for ( d = c + 1 ; d < n ; d++ )
        {
            if (array[position] > array[d])
                position = d;
        }
        if ( position != c )
        {
            swap = array[c];
            array[c] = array[position];
            array[position] = swap;
        }
    }
   
    printf("Sorted list in ascending order:\n");
   
    for ( c = 0 ; c < n ; c++ )
        printf("%d\n", array[c]);

}


#2. Posted:
Xaldin
  • 2 Million
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
You'll want to declare the array in main and pass it to the function for sorting instead of declaring the array in the function itself. (code in main)
int array[100];
sortArray(array);
//...

function doesn't have to return anything because arrays are passed by reference (technically in C they're passed as a pointer to the first element but for your uses same thing) so keeping it void should be fine. Don't forget to change the prototype to allow for an array parameter too.
#3. Posted:
Almac14
  • 2 Million
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Ok so I took your advice I think but I still cant get the program to run like i could when I had everything in just main. Not sure what im doing wrong.


#include <stdio.h>
#include<time.h>                  // for time()
#include<stdlib.h>                //for rand()

void sortArray(int array[]);

int main()
{
    int x;
    int array[100];
   
    //srand(time(NULL));                       //random number generator
   
    printf("Enter number of elements\n");
    scanf("%d", &x);
   
    //printf("Enter %d integers\n", n);
   
    sortArray(array);
   
    return 0;
}

void sortArray(int array[])
{
    int c, n, d, position, swap;

   
    for ( c = 0 ; c < n ; c++ )
        array[c] = rand()%100;                 // storing a random number between 0 and 100
   
    for ( c = 0 ; c < ( n - 1 ) ; c++ )
    {
        position = c;
       
        for ( d = c + 1 ; d < n ; d++ )
        {
            if (array[position] > array[d])
                position = d;
        }
        if ( position != c )
        {
            swap = array[c];
            array[c] = array[position];
            array[position] = swap;
        }
    }
   
    printf("Sorted list in ascending order:\n");
   
    for (c = 0; c < n; c++)
        printf("%d\n", array[c]);

}


#4. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
pass your other variables to the function as well, you're re-declaring a variable like 'n' but it has no value variable n in main and variable n in the sort function are completely different thats why you pass those variable to the function.

In your function youre saying
int n;
 c<n


but n doesnt have a value so pass that in and any other variables you arent settting values for. lmk if you dont understand
#5. Posted:
Almac14
  • 2 Million
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
I feel like i know what you're trying to say but everything ive tried hasnt worked out for yet. I also cant find amy of my previous labs that might be able to reference this exact problem which is a huge bummer.
#6. Posted:
Xaldin
  • 2 Million
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
you need some value for n in your function, either by passing or declaration.
is x in main supposed to be what n is in the function?
seems like you should be calling
sortArray(array, x);

and remove the n declaration and replace it with x

seems like youre capturing x(number of elements in main) but then you arent doing anything with it? then youre declaring int n in the function and using it for what x should be? seems like if you pass in x it should be fine.

#include <stdio.h>
#include<time.h>                  // for time()
#include<stdlib.h>                //for rand()

void sortArray(int array[], int);

int main()
{
    int x;
    int array[100];
   
    //srand(time(NULL));                       //random number generator
   
    printf("Enter number of elements\n");
    scanf("%d", &x);
   
    //printf("Enter %d integers\n", n);
   
    sortArray(array, x);
   
    return 0;
}

void sortArray(int array[], int x)
{
    int c, d, position, swap;

   
    for ( c = 0 ; c < x ; c++ )
        array[c] = rand()%100;                 // storing a random number between 0 and 100
   
    for ( c = 0 ; c < ( x - 1 ) ; c++ )
    {
        position = c;
       
        for ( d = c + 1 ; d < x ; d++ )
        {
            if (array[position] > array[d])
                position = d;
        }
        if ( position != c )
        {
            swap = array[c];
            array[c] = array[position];
            array[position] = swap;
        }
    }
   
    printf("Sorted list in ascending order:\n");
   
    for (c = 0; c < x; c++)
        printf("%d\n", array[c]);

}
#7. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
sorry for the many edits, I left a copy of your full code changed to what I think is your problem
#8. Posted:
Almac14
  • 2 Million
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Quick question, i need to be able to delete duplicates from my two arrays. I looked up examples and checked some slides but not sure what i am doing wrong and any help would be appreciated. The code I came up with is :

for(c = 0; c < x; c++)
    {
        for(d = 0; d < count; d++)
        {
            if(array[c] == arrayb[d])
                printf("here");
                break;
        }
        if(d == count)
        {
            arrayb[count] = array[c];
            count++;
        }
    }


My problem is i think im writing it wrong with my declared variables and so im not really sure how to fix that and also this would be written in main.
#9. Posted:
Xaldin
  • TTG Addict
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
duplicates within one array or the same number cant be in 1 array or the other?

for(c = 0; c < x; c++)
    {
   check = true;
   d= 0;
        while(d < count && check == true )
        {
            if(array[c] == arrayb[d]){
            check = false;
            position = d;
            }
         else{
         d++;
         }   
        }
      if(check == false){
         array[position] = array[x-1]; //If you dont care about the order of the elements this works.
         x--;
      }
    }

after running this array will no longer have any values that array b has(I think, didnt test if not its close lol) im pretty much checking to see if an element matches and leaving the loop once it does using check. This code will change the order of your array if thats a problem then you cant use it
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.