LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-30-2004, 04:44 AM   #1
dmigh
LQ Newbie
 
Registered: Oct 2004
Posts: 29

Rep: Reputation: 15
[C prog] how to do this?


Hi,

for int arrays like this
int a[5]={10,10,10,10};

now how can i insert 0 to the arrays a? like this.

a_afterchange[]={10,0,10,0,10,0,10,0,10,0};

thx for your time.

--
 
Old 10-30-2004, 05:21 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
This way, you can not.
 
Old 10-30-2004, 05:52 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
and that way, you can:
Code:
  int a[5]={10,10,10,10};
  int i;
  int alen=sizeof(a)/sizeof(int);
  int *a_afterchange=(int *)malloc(2*sizeof(a));
  for(i=0; i<2*alen;i+=2)
  {
    a_afterchange[i]=a[i/2];
    a_afterchange[i+1]=0;
  }
As Hko pointed, you cannot reuse the a variable, which size is fixed at compile time.
 
Old 10-30-2004, 06:19 AM   #4
dmigh
LQ Newbie
 
Registered: Oct 2004
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks jlliagre.

what should i have to reach such a level?
i have to more typing c sources.
can you recommend any book to me?

--
 
Old 10-30-2004, 06:46 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,492

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
There's only one book: The C programming language by k&r.
http://cm.bell-labs.com/cm/cs/cbook/
 
Old 10-30-2004, 07:00 AM   #6
dmigh
LQ Newbie
 
Registered: Oct 2004
Posts: 29

Original Poster
Rep: Reputation: 15
for this int arrays a,
int a[8]={10,9,10,8,};

then how to do this? note that 0 must be inserted behind of 10. not 9, 8.

a[8]={10,0,9,10,0,8,};

Last edited by dmigh; 10-30-2004 at 09:31 AM.
 
Old 10-31-2004, 02:36 AM   #7
dmigh
LQ Newbie
 
Registered: Oct 2004
Posts: 29

Original Poster
Rep: Reputation: 15
s

Hi,
Here is my pain. how to generalize it? i.e., put it in while loop..

--


Code:
#include <stdio.h>

int main()
{
        int a[8]={10,9,10,8,};
        int i;
        int alen=sizeof(a)/sizeof(int);

        int *a_afterchange=(int *)malloc(sizeof(a));

        for(i=0; i<alen;i++)
        {
                a_afterchange[i]=a[i];
                if(a_afterchange[i]==10)
                {
                        a_afterchange[++i]=0;
                        break;
                }

        }

        i++;


        for(;i<alen;i++)
        {
                a_afterchange[i]=a[i-1];

                if(a_afterchange[i]==10)
                {
                        a_afterchange[++i]=0;
                        break;
                }

        }

        i++;

//        printf("\n%d\n",i);

        for(;i<alen;i++)
        {
                a_afterchange[i]=a[i-2];

                if(a_afterchange[i]==10)
                {
                        a_afterchange[++i]=0;
                        break;
                }

        }

        for(i=0;i<alen;i++)
        printf("%d ",a_afterchange[i]);

        printf("\n");
        return 0;
}
 
Old 10-31-2004, 03:45 AM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
What about something like this?
Code:
#include <stdio.h>

int main(void)
{
  int a[13] = { 10, 9, 10, 8, 10, 7, 10, 6 };
  int i, j, n_added = 0;

  for(i = 0;i < 8+n_added;++i)
  {
    if(a[i] == 10)
    {
      for(j = 7+n_added;j >= i+1;--j)
        a[j+1] = a[j];

      a[i+1] = 0;
      n_added++;
    }
  }

  for(i = 0;i < 8+n_added;++i)
    printf("%d ", a[i]);
  putchar('\n');

  return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cd prog ahmed gamal Linux - Software 1 11-14-2005 06:55 AM
Help with my prog. Slickmink Programming 7 08-25-2005 10:50 AM
I want to start a prog from another prog but not as child grupoapunte Programming 5 05-23-2005 05:37 PM
Better buying "advanced linux prog" or "unix advanced prog" Dominik Programming 3 12-31-2003 01:11 PM
Can anyone help me with a C prog I wrote? WorldBuilder Linux - Software 10 10-23-2003 09:11 AM


All times are GMT -5. The time now is 03:23 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration