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;
}
Related
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 10 days ago.
The community is reviewing whether to reopen this question as of 10 days ago.
Improve this question
I know strings end with '/0' in C. While I'm dynamically allocating memory for a string, how do I handle that?
Also, how would you print out a dynamically allocated string? Because I've tried the regular way to print out a string and it did not work.
For the first question, I tried:
int len;
scanf("%d", &len);
char* str = (char*)malloc(sizeof(char)*len);
for (int i = 0; i<len; ++i){
scanf("%c", (str+i));
}
For the second question, I tried
printf("%s", *str);
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.
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++;
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 8 years ago.
Improve this question
#include <stdio.h>
int main()
{
int var=0;
for(; var++; printf("%d",var));
printf("%d", var);
}
Please explain to me this C code. How is the output 1?
You might be confused because of the wrong code indentation. Your code is:
for(; var++; printf("%d",var))
;
printf("%d", var);
So you always get the output of the second printf. As var is initialized to 0 and var++ (the for-condition) is always executed, you end up with var==1.
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;