Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0

    Hey a little help


    Hey all

    im new to the forum so hey again.

    im also relatively new to c programming and am just stuck on something if someone could check this out and help me understand why it isnt working id appreciate it allot.
    its to do with function calling and arrays.

    ok and how do i upload the file??
    or i could email it to u

    thanks
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    You can post the code here inline, simply by doing the following.

    1. Write the word [code]
    2. Paste your code direct from your text editor
    3. Write the word [/code]
    4. Press the "preview post" button to make sure your code looks like this
    Code:
    int main ( ) {
        printf("hello world\n");
        return 0;
    }
    and not like this
    int main ( ) {
    printf("hello world\n");
    return 0;
    }

    5. When you're happy with the post, then press "submit".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void AssignRand( char array[], int size );           // function for assigning randomized characters to the array
    void DisplayArray( char array[], int size );
    
    int main(void)
    {
    	char array[100];        // character array of 100 elements
    	int size;               // element number of array
    
    	AssignRand( array[100], size );
    	DisplayArray( array[100], size );
    
    	return 0;
    }
    
    void AssignRand( char array[], int size )
    {
    	int j;    // randomized integer
    	srand( time(NULL));
    
    	for ( size=0; size<100; size++ )
    	{
    		j= 97+rand();
    		array[size]= (char)j;
    	}
    	return;
    }
    void DisplayArray( char array[], int size )
    {
    	printf ( "Displaying a randomly generated character array with 100 blocks:\n%s%13s\n", "Element", "Value" );
    
    	for ( size=0; size<100; size++ )
    	{
    		printf ( "%7d%13s", size, array[size] );
    	}
    	return;
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    im trying to make functions which assign random letters from a-z to my array of block 100 and then display them but using .

    the 1st function after main is for assigning random letters and the second is for printing out
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Do you have a question about your code? Please ask specific questions and when you have compiler error/warning messages be sure to provide those as well.

    I do see a few things wrong, you're not properly terminating your C-string in your AssignRand() function. Remember C-strings must end with the end of string character '\0'. And don't forget to leave space for this character in your loop.

    Another thing in your DisplayArray() function you only have one C-string why are you using a loop. Remember the "%s" printf() specifier is to print a string not a character.

    Also you may want to review your documentation on how to pass an array to a function. Your function calls in main() are wrong.


    Jim
    Last edited by jimblumberg; August 25th, 2013 at 10:29 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    ok i think ive sorted everything out i was meant to display everything out now so specificly how do i call in these functions?

    im not understanding my documentation

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void AssignRand( char array[], int size );           // function for assigning randomized characters to the array
    void DisplayArray( char array[], int size );
    
    int main(void)
    {
    	char array[100];        // character array of 100 elements
    	int size;               // element number of array
    
    	AssignRand( array[100], size );
    	DisplayArray( array[100], size );
    
    	return 0;
    }
    
    void AssignRand( char array[], int size )
    {
    	int j;    // randomized integer
    	srand( time(NULL));
    
    	for ( size=0; size<100; size++ )
    	{
    		j= 97+rand()%25;
    		array[size]= (char)j;
    	}
    	return;
    }
    void DisplayArray( char array[], int size )
    {
    	printf ( "Displaying a randomly generated character array with 100 blocks:\n%s%13s\n", "Element", "Value" );
    
    	for ( size=0; size<100; size++ )
    	{
    		printf ( "%7d%13c", size, array[size] );
    	}
    	return;
    }
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    i knw the function calls in my previous post are wrong and im not sure how they are wrong
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Let's look at your function signatures:
    Code:
    void AssignRand( char array[], int size );
    This is saying you're going to pass to this functions two parameters. The first parameter will be an array of characters. The second will be an int, this int will be passed by value.

    Now lets look at the function call:
    Code:
    	int size;               // element number of array
    
    	AssignRand( array[100], size );
    Here you are passing a single character, not an array and an uninitialized integer. When you pass an array to a function you are actually passing a pointer to that array. This is accomplished by using the name of the variable without the [].

    Now since your passing size into the function by value any changes made to size in your function are lost when the function returns. You should probably initializing size to be the size of your array. Then use this size in your loops inside your functions instead of the 100.

    Jim
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    ok so ive landed up with this but i dnt think my sort function isnt working properly as its not displaying the characters in alphabetical order as it should

    Code:
    #include <stdlib.h>
    #include <time.h>
    
    void AssignRand( char array[], int size );           // function for assigning randomized characters to the array
    void DisplayArray( char array[], int size );
    char BubbleSort( char array[] );
    
    int main(void)
    {
    	char array[100];        // character array of 100 elements
    	int size=0;               // element number of array
    
    	AssignRand( array, size );
    	DisplayArray( array, size );
    	BubbleSort( array );
    	DisplayArray( array, size );
    	return 0;
    }
    
    void AssignRand( char array[], int size )
    {
    	int j;    // randomized integer
    	srand( time(NULL));
    
    	do
    	{
    		j= 97+rand()%25;
    		array[size]= (char)j;
    		size++;
    	}while(size<100);
    
    	return;
    }
    void DisplayArray( char array[], int size )
    {
    	printf ( "Displaying a randomly generated character array with 100 blocks:\n%s%13s\n", "Element", "Value" );
    
    	do
    	{
    		printf ( "%7d%13c\n", size, array[size] );
    		size++;
    	}while(size<100);
    
    	return;
    }
    char BubbleSort( char array[] )
    {
    	int pass;
    	for( pass=0; pass<99; pass++ )
    	{
    		int hold;                 //temporary position used to swap elements
    		if( array[pass] > array[pass+1] )
    		{
    			hold = array[pass];
    			array[pass]=array[pass+1];
    			array[pass+1]=hold;
    		}
    
    	}
    
    	return array;
    }
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0
    Maybe you should check your sorting function. The bubble sorting needs two loops.

    Sorry for my terrible english :).
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    thanks for all the help guys this program is sorted out...but do expect more haha.

    here is the finished product:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void AssignRand( char array[], int size );           // function for assigning randomized characters to the array
    void DisplayArray( char array[], int size );
    void BubbleSort( char array[], int size );
    
    int main(void)
    {
    	char array[100];        // character array of 100 elements
    	int size=100;               // size of array to be passed through functions
    
    	AssignRand( array, size );            // function calls
    	BubbleSort( array, size );
    
    	return 0;
    }
    
    void AssignRand( char array[], int size )
    {
    	int k;    // randomly generated integer
    	int j=0;   // counter for element character assignment
    	srand( time(NULL));
    
    	for( j=0; j<size; j++ )
    	{
    		k= 97+rand()%25;        // generates the random integer
    		array[j]= (char)k;          // changes integer to character (a-z)
    	}
    	DisplayArray( array, size );   // displaying array
    
    	return;
    }
    void DisplayArray( char array[], int size )
    {
    	int i;
    	printf ( "Displaying a randomly generated character array with 100 blocks:\n%s%13s\n", "Element", "Value" );
    
    	for( i=0; i<size; i++)
    	{
    		printf ( "%7d%13c\n", i, array[i] );
    	}
    
    	return;
    }
    void BubbleSort( char array[], int size )
    {
    	int pass;     // counter for each sorting pass
    	for( pass=0; pass<size; pass++ )
    	{
    		int f;    //
    		for( f=0; f<size-1; f++)
    		{
    			int hold;                 //temporary position used to swap elements
    			if( array[f] > array[f+1] )
    			{
    				hold = array[f];
    				array[f]=array[f+1];
    				array[f+1]=hold;
    			}
    
    		}
    	}
    	DisplayArray( array, size );     // displaying array
    	return;
    }
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    omw...guys whenever i run my programs 2 consoles are appearing on in the top right and another in its own tab/window its so annoying how do i get to just display the one at the bottom???
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    nvm got it
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    19
    Rep Power
    0
    hey guys once again something is wrong...okay im trying to scan values into a 2D float array which is 2 by 3 blocks. the problem is for some reason my program reads in 4 valus for the 1st row and 3 values for the 2nd row when it should just be 3 and then 3 again.
    here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void getPositionForce(float matrix[][3], int numRows);
    
    int main()
    {
    	float matrix[2][3];
    	int rows = 2;
    
    	getPositionForce(matrix, rows);
    
    
    	return 0;
    }
    
    void getPositionForce(float matrix[][3], int numRows)
    {
    	int f;   //counter
    	int g;   //counter
    
    	printf( "Determinant: |  i  j  k  |\n             |  rx ry rz |\n             |  Fx Fy Fz |\n" );
    
    	for( f=0; f<numRows; f++ )
    	{
    		printf( "\nEnter vector %d ( type in each component and hit enter )\n\n", (f+1) );
    
    		for( g=0; g<3; g++ )
    		{
    			scanf( "%f \n", &matrix[f][g] );
    		}
    
    	}
    
    	return;
    }
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Any time you have characters other than the format specifiers in your scanf() call you'll need to enter those characters as well. Try removing the space and newline character.

    Jim
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo