ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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;
}
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.