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.
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 2 years ago.
Improve this question
If I have a string, say "abcdefghij"
How can I iterate and get to characters backwards at a time.
For example
first loop : ghij
second loop: fedc
third loop: ab
Im using C++ but dont feel like its simple enough i should be able to adapt any language
Heres what I have so far:
for(long unsigned int i=0; i<s.length();i+=4){
digits.push_back(std::stoi(s.substr(i, 4)));
}
My issue is that this is left justified, not right justified
In pseudo code:
iterate i from s.length stepping by -4, while i > 0
start = max(i - 4, 0)
part = s.substring(start, i)
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Only been coding in C for about a month, but wondered why the while loop wasn't used more often for FIZZBUZZ. Is it not as clean or some other reason? Here's what I came up with:
int main()
{
int num=1;
while(num<=100)
{
if (num%3==0 && num%5==0)
{
printf("FIZZBUZZ!!!\n");
}
else if (num%3==0)
{
printf("FIZZ!!!\n");
}
else if (num%5==0)
{
printf("BUZZ!!!\n");
}
else
{
printf("%d\n", num);
}
num++;
}
return 0;
}
Your loop can be neatly folded into a for loop:
for(int num = 1; num <= 100; ++num)
There are two advantages here:
num is scoped inside the loop, when before it was bleeding into whatever scope followed the while. Restricting variables to the minimum possible scope is a good rule of thumb, because it minimizes the amount of variables to think about at any given point.
The range over which your program will operate is now summarized in a single place: you can see the bounds (1 to 100) and the step (1) inside the for. That kind of ranges is pretty well balanced to be read and memorized quickly before reading the body of the loop. For example, if you now wanted to only check odd numbers, it would be immediately clear from reading the num += 2 in the for header, rather than stumbling upon it at the very end of the loop's body.
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];
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 8 years ago.
Improve this question
What are the cases in which a loop cannot be unrolled? I've been reading a paper which shows a loop that it says is not able to be unrolled. I cannot actually post the specific code as it is private, however, I am wondering if there is something obvious I am missing in regards to not being able to unroll.
Thanks in advance. If there's any other info that I can try to provide, let me know.
well you can't unroll a loop with any type of recursion in it because it could be infinitely long, also you cant unroll an infinite loop or one with some kind of method for breaking out that isn't incremental
recursion:
method(int x){
if(x > 0)
return 0;
else
return method(x-1);
infinite loop:
while(true){
...
if(some condition)
break;
}
last one:
boolean somevar = true;
while(somevar){
...
if(some condition)
somevar = false;
}