Thread: Program moving values in vectors... need help.

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    5

    Program moving values in vectors... need help.

    I have written a program that, should move values of a given vector, but id does nothing after declarating for how many places move te values. If you could check the code and write whatis wrong, i would be grateful.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int przesun(int t[], int p);
    
    void main()
    {
    	int k;
    	int w[10];
    	printf ("Define, how many places you want to move values: ");
    	scanf ("%d\n",&k);
    	przesun (w,k);
    }
    
    int przesun (int t[], int p)
    {
    	int i;
    	for (i=0 ; i<10 ; i++)
    	{
    		t[i]=rand();
    		printf("%d-th element of array is equal %d\n",i,t[i]);
    	}
    	for (i=9 ; i>=0 ; i--)
    	{
    		t[i]=t[i-p];
    		printf("&d-th after moving for %d places is equal %d\n",i,p,t[i]);
    		if(i-p<0)
    		{
    			t[i]=t['0'];
    		}
    	}
    	return 0;
    }

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That's an array.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main()
    main returns an int.
    See the FAQ

    > t[i]=t['0'];
    You want 0, not '0'
    If ASCII is your character set, then you've just read t[48], which is way outside your array.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    scanf ("%d\n",&k);
    
    instead
    
    scanf ("%d",&k);
    ssharish2005

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Thanks for Your help. The code in such form is fully correct:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int przesun(int t[], int p);
    
    main()
    {
    	int k;
    	int w[10];
    	printf ("Define, how many places you want to move values: ");
    	scanf ("%d",&k);
    	printf("\n");
    	przesun (w,k);
    }
    
    int przesun (int t[], int p)
    {
    	int i;
    	for (i=0 ; i<10 ; i++)
    	{
    		t[i]=rand();
    		printf("%d-th element of array is equal %d\n",i,t[i]);
    	}
    		for (i=9 ; i>=0 ; i--)
    	{
    		if((i-p)<=0)
    		{
    		t[i]=0;
    		printf("%d-th after moving for %d places is equal %d\n",i,p,t[i]);
    		}
    		else
    		{
    		t[i]=t[i-p];
    		printf("%d-th after moving for %d places is equal %d\n",i,p,t[i]);
    		}
    	}
    	return 0;
    }
    BUT I have one more question... how to define lenght of an array by giving a value? I mean I define an integer, write its value, and this integer is a length of an array.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    BUT I have one more question... how to define lenght of an array by giving a value? I mean I define an integer, write its value, and this integer is a length of an array
    you cant declare an array size at the run time. what u can do is use dynamic memeory allcation. sample code

    Code:
    #include<stdio.h>
    
    int main()
    {
        int *p;
        int size,i;
        
        printf("Array size : ");
        scanf("%d",&size);
        
        p = malloc(sizeof(int) * size);
        
        printf("Enter array values\n");
        for(i=0;i<size;i++)
            scanf("%d",&p[i]);
        
        printf("Array elements\n");   
        for(i=0;i<size;i++)
            printf("%d\t",p[i]);
        
        getchar();
        return 0;
    }
    /*myoutput
    Array size : 5
    Enter array values
    45
    85
    35
    12
    45
    Array elements
    45      85      35      12      45
    */
    ssharish2005

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Frankly, i don't understand what you mean. When I tried to use thiś malloc function, errors occured while compiling and nothing worked.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you could
    a) stop using a C++ compiler to compile C code
    b) say something more useful that "it doesn't work" or "I get error messages".

    By the way, ssharish2005 forgot to
    #include <stdlib.h>
    Perhaps that fixes one possible error message (if the error message was along the lines of converting an int to a pointer say)

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    Compiler writes following message : "error C2440: '=' : cannot convert from 'void *' to 'int *'" concerning such line of code :"p = malloc(sizeof(int) * size)".

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    5
    And the error occurs even despite including stdlib.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > cannot convert from 'void *' to 'int *'"
    Which means you're compiling your C code with a C++ compiler.

    Rename your prog.cpp to be prog.c

  12. #12
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    And you should always check the return value of malloc, like so:
    Code:
    if((p = malloc(sizeof(int) * size)) == NULL)
        exit(1); 
        /* And print a useful error message if you want */
    In practice, malloc does not fail very often - for instance, it will fail if you run out of memory which for a lot of programs isn't very likely. However, if it does fail, it's definitely a bad thing and you should deal with it appropriately.

    Also, if you've never used it before, it's worthwhile reading the documentation rather than just copy-pasting example code people give you. Even if the code is correct, you should take the time to understand the functions you are using.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How to complete program. Any probs now?
    By stehigs321 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 04:03 PM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM