How does the code that prints array elements works? [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}

Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

Related

how to get random questions picked by machine in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
i am making a program in C where i ask the user 10 questions and i have a list of 20 questions. I want the machine to randomly pick and ask any 10 questions. Can anyone help me how to do that?
As an outline:
struct Q {
char *q;
int hasbeenselected;
} q[20];
int n;
//
// fill q with your questions and set hasbeenselected to 0;
srand(time(NULL));
for (int i=0; i<10; i++) {
do {
n= rand()%20;
} while (q[n].hasbeenselected==1);
// now ask question q[n].q
q[n].hasbeenselected= 1;
}

Incrementing constant variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
#include <stdio.h>
#define number 0
main()
{
int i;
for (i = 0; i < 5; i++)
{
number++;
}
printf("Number is: %d", number);
}
No, 0 is substitued for number by the preprocessor before the compiler gets to work.
The compiler issues a diagnostic when it sees 0++;

How to select data randomly in 2-d array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to get some data randomly from an 2-d array.
In my partial code given below where cluster_center and sample data both are 2-d array in double type. I want to assign some data randomly from sample_data array to cluster_center array.
for(int i= 0; i< 3; i++)
{
for(k=0; k<17; k++)
cluster_center[i][k] = //what will be???;
}
TIA :)
You could just generate two random numbers via rand and modulate it to ensure it doesn't exceed your 2-D array boundaries. Not sure exactly how random you need it to be though, as rand will favor lower numbers slightly according to the man page.
You'd assign the value like:
cluster_center[i][k] = sample_data[random_num1][random_num2];

When to declarate a variable issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Can someone explain me the most simple way the meaning of this syntax of C?
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(number[i]<number[j])
{
aux=number[i];
number[i]=number[j];
number[j]=aux;
}
}
}
I just trying to figure out I know is an iteration or a loop but specifically aux is a var. Why i need to follow this i'm trying to get pos and negs, into an array but this part i'm stuck is there another way ?
I just need to figure this syntax.
This looks like Bubble Sort. aux is a temporary variable used for exchanging the values of number[i] and number[j]. You can't do
number[i] = number[j];
number[j] = number[i];
to exchange the two, as both would be equal to number[j] this way. So you need a temporary variable.

convert 4*4 int array into 2*2 int array in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a 4*4 array-
int arr[4][4] = {{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
want to convert this into 2*2 array -
int final[2][2];
that should have
final[0][0] = (arr[0][0]+arr[0][1]+arr[1][0]+arr[1][1])/4;
final[0][1] = (arr[0][2]+arr[0][3]+arr[1][2]+arr[1][3])/4;
Is it possible to extract the value from array using lookup table.
Thanks in advance
As has been well commented, you basically have this, but just for completeness:
for ( i=0; i<2; i++ )
for ( j=0; j<2; j++ )
final[i][j] = (arr[i*2][j*2]+arr[i*2][j*2+1]+arr[i*2+1][j*2]+arr[i*2+1][j*2+1])/4;

Resources