LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 11-03-2003, 06:14 PM   #1
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Rep: Reputation: 15
how can i do it?


#include <stdio.h>
#define SizeA 30

main() {
int i,k,j;
int argv1[SizeA], argv2[SizeA] ;


for(j=0;j<SizeA;j++) {

printf("Array uno inizializzato con i primi 30 numeri\n\n");
break;
}
for( i=1, k=30; i<(SizeA/2), k>(SizeA/2); i++, k--) {

argv2[SizeA]=argv2[i,k];


printf("%d,%d;", i, k);

}


return(1);

}



I want put i and k like elements of argv2 so at last i'll print 1,30;2,29;3,28;4,27;5,26;6,25;7,24;8,23;9,22;10,21;11,20;12,19;13,18;14,17;15,16;


how can i do it?
 
Old 11-03-2003, 06:15 PM   #2
linuxanswer
Member
 
Registered: Oct 2003
Distribution: woodY 3.0 stable
Posts: 61

Original Poster
Rep: Reputation: 15
that 's to say, it's correct argv2[SizeA]=argv2[i,k];?
 
Old 11-03-2003, 06:21 PM   #3
djtoltz
Member
 
Registered: Nov 2003
Location: Eastern North Carolina, USA
Distribution: Mandrake
Posts: 51

Rep: Reputation: 20
This is a "C" programming question, not really a Linux question. You've got some issues with your C syntax, for starters. However, you don't need the arrays for what you are trying to do. All you really need is two variables that start at min and max and meet in the middle.

Something like the following should work...

#include <stdio.h>

int main() {
int i, j;
for (i=1, j=30; i < j; i++, j--)
{
printf("%d,%d,",i,j);
}
return 0;
}
 
Old 11-03-2003, 06:25 PM   #4
djtoltz
Member
 
Registered: Nov 2003
Location: Eastern North Carolina, USA
Distribution: Mandrake
Posts: 51

Rep: Reputation: 20
If you are trying to fill in an array with the numbers of your sequence, the syntax would be something like this;

/* initialize a_index to zero before this */
argv2[a_index++]=i;
argv2[a_index++]=j;

... this would put i and j into the array at the offset <a_index> which would continue to be bumped up as elements were added.
 
Old 11-03-2003, 07:28 PM   #5
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
I put some comments around your code to help you out.
Code:
#include <stdio.h>
#define SizeA 30	/* It is convention to put constants all in CAPS */

main() {
	int i,k,j;
	/* 
	* argv1 is unused and it is a convention to use
	* argv to refer the the values of
	* the arguments passed to your program
	* Argument Values hence argv
	*/
	int argv1[SizeA], argv2[SizeA] ; 

	/*
	* This loop only prints your string
	* once this is is silly
	*/
	for(j=0; j<SizeA; j++) {
		printf("Array uno inizializzato con i primi 30 numeri\n\n");
		break;
	}
	for( i=1, k=30; i<(SizeA/2), k>(SizeA/2); i++, k--) {
		/* 
		* SizeA is one index outside of the array and 
		* it makes no sense to loop assigning the same
		* variable to different values
		*/
		argv2[SizeA]=argv2[i,k]; 
		printf("%d,%d;", i, k);
	}
	return (1); /* Zero is customary for indicating success */
}
My version would look like
Code:
#include <stdio.h>
#define SIZE_A 30

int main(int argc, char *argv[])
{
        int i;
        int array0[SIZE_A];
        int array1[SIZE_A];

        /*
        * I don't know why I do this. Your program
        * just says that is what it is doing so I
        * actually do it.
        */
        printf("Initialize array with first 30 numbers\n");
        for (i = 0; i < SIZE_A; i++) {
                array0[i] = i;
        }


         for (i = 0; i < SIZE_A; i += 2) {
                array1[i] = (i+2) / 2;
                array1[i+1] = SIZE_A - (i/2);
                printf("%d,%d;", array1[i], array1[i+1]);
        }

        printf("\n");
        return 0;
}
Here you go and good luck.

Last edited by LogicG8; 11-03-2003 at 08:20 PM.
 
  


Reply



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



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration