Help with field index!

This is a discussion on Help with field index! within the C Programming forums, part of the General Programming Boards category; I need to put numbers from fieldA to even indexes of fieldC, and numbers from fieldB to uneven indexes of ...

  1. #1
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78

    Help with field index!

    I need to put numbers from fieldA to even indexes of fieldC, and numbers from fieldB to uneven indexes of fieldC. Im just going in circles, need some hints! I filled up fieldC with nums from A and B but dont know how to sort them like this.

    need this: 1. fieldA, 2. fieldB, 3.fieldA, 4. fieldB...

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I really don't understand what you mean by field index... and btw, the plural of index is indices.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    I have an array of 10 elements. I created it from an 2 arrays of 5 elements each. I have array 'A' which is filled up with elements from arrays 'B' and 'C'.

    A1.B1
    A2.B2
    A3.B3
    A4.B4
    A5.B5
    A6.C1
    A7.C2
    A8.C3
    A9.C4
    A10.C5

    And I need array 'A' to have array 'B' on its even indices and array 'C' on its uneven indices.


    A1.C1
    A2.B1
    A3.C2
    A4.B2.....

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    aha! That makes more sense. There's literally tons of ways of doing this.
    You can use one loop and mod the counter to see whether it's even or odd.
    You could use two loops and multiply the counter by 2 when assigning to A and for one of the loops you add one for the other you don't.
    That should get you started. Knock up some code and post it here and then we can discuss what else needs to be done.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    #include <stdio.h>
    
    #define N 5
    
    int main()
    
    {
    	int sranje;
    	int prvi[N];
    	int drugi[N];
    	int a;
    
    	for(a=0;a<N;a++)
    	{
    		printf("%d. = ",a+1);
    		scanf("%d", &prvi[a]);
    	}
    
    	printf("\n");
    
    		for(a=0;a<N;a++)
    	{
    		printf("%d. = ",a+1);
    		scanf("%d", &drugi[a]);
    	}
    
    		function(prvi, drugi);
    
    	scanf("%d", &sranje);
    
    }
    
    int function(int prvi[N], int drugi[N])
    {
    	int *treci, M,a,k=0;
    	M=N+N;
    
    	treci=((int*)malloc(sizeof(int)*(M)));
    
    	for(a=0;a<N;a++)
    	{
    		treci[k]=prvi[a];
    		k++;
    	}
    	for(a=0;a<N;a++)
    	{
    		treci[k]=drugi[a];
    		k++;
    	}
    
    	for(a=0;a<M;a++)
    	{
    		printf("\n%d. = %d", a+1, treci[a]);
    	}
    	
    }
    I have this...

    i tried
    Code:
    for(a=0;a<N;a++)
    	{
    		if(a%2==0)
    		{
    		treci[k]=prvi[a];
    		}
    		k++;
    		
    	}
    , or putting
    Code:
    for(a=0;a<N;a++)
    	{
    		treci[k]=prvi[a];
    		k++;
    a++;
    	}
    to skip one extra number but nothing, I think it shoul be there somewhere..

  6. #6
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    
    {
    	int sranje;
    	int prvi[]={'2','2','2'};
    	int drugi[]={'5','5','5'};
    	int *pp,a,*pd,*pt;
    	int treci[6];
    	pp=prvi;
    	pd=drugi;
    	pt=treci;
    		
    		for(a=0;a<3;a++)
    		{
    			printf("%3d", pp[a]);
    		}
    		printf("\n");
    		for(a=0;a<3;a++)
    		{
    			printf("%3d", pd[a]);
    		}
    		for(a=0;a<6;a++)
    		{
    			pt=pp;
    			pt++;
    			pt=pd;
    		}
    		for(a=0;a<6;a++)
    		{
    			printf("\n %d.= %d",a+1, pt[a]);
    		}
    
    	scanf("%d", &sranje);
    }
    or something like, this, but it gives me strange output....50,53...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    		for(a=0;a<6;a++)
    		{
    			pt=pp;
    			pt++;
    			pt=pd;
    		}
    What's this supposed to do?

    Because it doesn't appear to me that it does anything. Each loop takes the copies the value of pp into pt, then adds one to pt, then copies pd into pt - the changed pt is not used.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    	for(a=0;a<6;a++)
    		{
    			for(b=0;b<3;b++)
    			{
    				pt[a]=pp[b];
    				
    			}
    			a++;
    			
    		}
    now, i managed to put pp into pt in every other element, still need to put pd in.

  9. #9
    Sometimes so stupid... shardin's Avatar
    Join Date
    Jul 2007
    Location
    Dalmatia/CRO
    Posts
    78
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    
    {
    	int sranje;
    	int prvi[]={'2','2','2'};
    	int drugi[]={'5','5','5'};
    	int *pp,a,*pd,*pt,b;
    	int treci[6];
    	pp=prvi;
    	pd=drugi;
    	pt=treci;
    		
    		for(a=0;a<3;a++)
    		{
    			printf("%3d", pp[a]);
    		}
    		printf("\n");
    		for(a=0;a<3;a++)
    		{
    			printf("%3d", pd[a]);
    		}
    		for(a=0;a<6;a++)
    		{
    			for(b=0;b<3;b++)
    			{
    				pt[a]=pp[b];
    				
    			}
    			a++;
    		}
    			for(a=1;a<6;a++)
    		{
    			for(b=0;b<3;b++)
    			{
    				pt[a]=pd[b];
    				
    			}
    			a++;
    		}
    			for(a=0;a<6;a++)
    		{
    			printf("\n %d.= %d",a+1, pt[a]);
    		}
    
    	scanf("%d", &sranje);
    }
    I did it!!!! HURA!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21