can someone help me with this logic

Posted on 2009-04-30
Last Modified: 2012-05-06
im still quite new in writting C so this might be a silly question

what im trying to do is:

have 2 arrays for example




and based on the numbers from array a[] use them as the locations for numbers in array b
by taking pairs from the first array

so here we have a[]'s first 2 numbers 0 and 1

then the number from b[][] = 2

the second pair from a[] is 1 and 2

then b[][]=5 and so on

any ideas how should i do that?

ultimately i want to find the sum based on the sequence i gave it in a[]
but i think i know how to do that bit

thanks :)
Question by:goofy26
    LVL 84

    Accepted Solution

    LVL 45

    Expert Comment

    Hi Goofy,

    You'll probably want a little different structure for b[][] as to C, it's nothing more than a memory region.  You're probably thinking that b[][] is a two-dimensional array like b[4][4].  But since the size isn't declared, C can't index into your array.

    There are a couple of ways around this.  One is to define b[][] as a fixed size array, such as b[4][4].  The other is to define *b* as an array of pointers such as **b or *b[].  You'd then use malloc() to assign memory to each row of data.  C will figure that out and let you index into it, but setting it up as constant/static memory (initialized by the compiler) is challenging.

    LVL 1

    Author Comment

    oh yes, b's size would be fixed sorry i forgot to mention that...

    i will try ur suggestion ozo thanks :)
    LVL 1

    Author Comment

    thanks this is what i couldnt think of :)

    i made it do the sum as well :)

    thanks a lot to both
    #include "stdafx.h"
    #include <stdio.h>
    int _tmain(int argc, _TCHAR* argv[])
    	int a[4]={0,1,2,3};
    	int sum=0;
    	int i;
    	int k[4][4]={{ 0,12,10,11},
    				{12, 0, 5, 8},
    				{10, 5, 0, 7},
    				{11, 8, 7, 0}};
    		printf("Numbers are: %d\n",k[a[i]][a[i+1]]);
    	printf("sum is: %d\n",sum);
    	return 0;

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now