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'd like to use continue statement (parametrized) n times:
int n = 7;
while(running()) {
commandA;
if(something) continue; // <- not once, but n times
commandB;
...
}
I'd like to use something like for(int i=0; i<n; ++i) continue; but the continue should be applied to the outer (while) loop. I'd like to skip n passes of the while loop.
The purpose is to always execute commandA, but skip n times commandB if running() condition is satisfied.
Is it possible to code that in C?
You could use an extra variable, like this, if I understood correctly what you are trying to achieve:
#include <stdio.h>
int main(void) {
int max_skip = 7;
int i = 0;
int something;
while(i < 10) {
something = i % 2;
if(something && max_skip-- >= 0)
continue;
++i;
}
return 0;
}
You short circuiting will come into play (as I explained here), which will protect max_skip from decreasing.
One way would be:
int n = 7;
while(running()) {
commandA;
if (n) { --n; continue; }
commandB;
...
}
continue can only be used to jump to the next iteration of the loop it appears in. You can't parametrize it, and you can't make it apply to any other loop. You will have to rewrite your loop.
Related
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
Could anyone tell me the problem with my program? it is a program in C to find the number of Armstrong number between 1 and 500. I am a beginner in programming, thus would be open to learning.
#include <stdio.h>
int main()
{
int number,originalNumber,remainder,result=0;
number = 001;
while (number <= 500)
{
originalNumber=number;
while(originalNumber != 0)
{
remainder= originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if (result==number)
{
printf("%d is a Armstrong Number",number);
}
number++;
}
}
Could anyone tell me the problem with my program?
The problem is that result=0 is only initialized at the start of the program, while result must be set to 0 in each while (number <= 500) loop cycle.
You forgot to set reset result before entering the while(originalNumber != 0) { ... } loop. To prevent such easy mistakes, it is a good idea to define variables in the innermost block possible. If the content of a variable does not have to be "remebered" after exiting a block, you define the variable inside that block. (A block beeing the code between { and })
#include <stdio.h>
/* changed `int main()` to `int main(void)` */
int main(void)
{
for(int number = 1; number<= 500; number++)
{
/*
`number` has to be remebered each time we exit and reenter
this block, so we have to declare it outside.
`orginalNumber` and `result` can be "forgot" after each time we finish
this block so we declare them here
(Just remember that each iteration of the loop is an "entering" and
"exiting" of this block)
*/
int originalNumber = number;
int result = 0;
while(originalNumber != 0)
{
/*
`remainder` can be "forgot" at the end of this block, so we
declare it here
*/
int remainder= originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}
if (result==number)
{
/* Added a newline when printing */
printf("%d is a Armstrong Number\n",number);
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've been trying to learn C by myself for the past two days and I can't seem to get this program to run. Sorry for this probably trivial question, just starting out and can't seem to find a quick answer.
#include <stdio.h>
void chopper() {
int z = 0;
while (z < 10) {
printf("They equal and this code works!");
z++;
}
}
int main() {
int x = 0;
int flag = 0;
if (flag == 1) {
chopper();
}
for (int x; x < 10; x++) {
printf("%d\n", x);
if (x == 10) {
flag == 1;
}
}
return 0;
}
You have multiple issues.
flag == 1; inside if is useless. Probably you meant and want flag = 1;.
In the for loop, x is uninitialized.
The outer scope x is unused.Note
What you want is to rewrite the for loop statement as
for (x; x < 10; x++)
or,
for (; x < 10; x++)
to make use of the outer x variable. As per the code shown, you don't need two separate variables anyway.
Note: To understand more about scope, please refer to this previous Q&A.
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
DataCount is how many times at sorting numbers.
int* MakeMWData(int DataCount)
{
// make array
int* Data = (int*)malloc(DataCount*sizeof(int));
int number = 2;
int count = 0;
Data[0] = 1;
// input data
int i,j;
for( i = DataCount;; i/=2)
{
count++;
for( j = 1; j<DataCount;j++)
{
//merge sort worst
i think this isn't correct.
if(j%i == 0 && j %(i * 2) != 0)
{
Data[j] = number;
number++;
}
}
if(i==1)
break;
}
for( i = 0; i<DataCount ; i++)
{
if(Data[i] ==0)
Data[i] = number;
number++;
}
return Data;
}
Making worst Data in main function.
int* MergeData = MakeMWData(DataCount[i]);
The way mergesort works is dividing the array in two arrays, recursively (logn times), untill being able to compare pairs of elements. Then it merges the recursively created arrays also sorting them at the same time.
For some sorting algorithms (e.g. quicksort), the initial order of the elements can affect the number of operations to be done. However it doesn't make any change for mergesort as it will have to do exactly the same number of operations anyway: recursively divide into small arrays and then merge them back, in total Θ(nlogn) time.
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
I'm new to C Algorithm and come to ask for some help.
I hope to check whether or not an element exists in a table, can anyone give me some good algorithm? What I do is a cycle and a flag, then quit the cycle and verify the flag. But it looks like stupid, so I guess there would be more efficient algorithm. My code is following:
int j=0;
u8_t next_header[]={0x11, 0x22};
for(i = 0; i < sizeof(next_header); ++i)
{
if (buf[6] != next_header[i])
continue;
else
++j;
}
if(j == 0)
{
// execution
}
else
{
// execution
}
pack it in a function, so you can jump out of the loop using return as soon as the element is found:
int search_for_elements(int element)
{
int i;
u8_t next_header[]={0x11, 0x22};
for(i = 0; i < sizeof(next_header); ++i)
{
if (element == next_header[i])
return 1; // found the element;
}
return 0; // :( no element found
}
Yes, if your table is a simple array looping is the way to go. That said, the usual pattern is to use a break statement instead of a continue.
int found = 0;
for(i=0=; i<the_array_length; i++){
if(array[i] == the_element_i_am_searching){
found = 1;
break;
}
}
Also, its more idiamatic to test booleans with if(i) instead of if(i != 0). (That said, if your variable is a number that is not always 0 or 1, like your j , I would keep the explicit comparison for clarity)
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 9 years ago.
Improve this question
I'm taking a C language course,and the lecturer loves to give a very complex recursive programs in the exam and ask what the call of the function does.
I would like to know some methods/tips for this kind of problems analyze.
Here is a question for example:
what does the following prog? (There are no computers on the exam)
#include <stdio.h>
#include <conio.h>
int* what(int *arr, int *maxLen, int *curLen)
{
int *res;
if (*arr == -1){
*maxLen = *curLen = 0;
return arr;
}
if (arr[1] == -1 ){
*maxLen = *curLen = 1;
return arr;
}
if (arr[2] == -1 ){
*maxLen = *curLen = 2;
return arr;
}
res = what(arr+1, maxLen, curLen);
if ((arr[1] - arr[2]) * (*arr - arr[1]) > 0 ){
*curLen = *curLen + 1;
if ( *curLen >= *maxLen){
*maxLen = *curLen;
res = arr;
}
}else
*curLen=2;
return res;
}
void main(){
int cur, max, i, j, *res, a[][20]= {{5,-1}, {5,6,-1},
{1,4,5,9,7,6,4,6,9,8,5,4,1,4,5,6,-1}};
for(i=0; i<3; i++){
res = what(a[i], &max, &cur);
printf("max=%d, ",max);
for ( j = 0 ; j < max ; j++)
printf("%d ", res[j]);
('\n');
}
}//end
If you have to analyze recursive code without any documentation (uncommon!), one approach is to treat it as you would a proof-by-induction:
1) Find the base case (the case where it returns without recursing). Understand what it does in that situation, for all the possible branches.
2) Then look at the recursive case. Understand what it's doing for the next-to-last case, using your understanding of the base case.
If necessary, repeat (2) until you understand the code.
As with other coding conventions, these become easier to "read" with practice
Note that this question isn't really specific to the C language; recursion is used in most languages, when appropriate. Some -- the "functional" languages -- use it particularly heavily.
Try the program on paper..execute line by line,keeping record in a stack diagram and striking out and adding elements as removed and added. Simple example is for finding factorial by recursion.
3!=3*(2!)->3*(2*(1!))->3*(2*(1)) returns 3*2->6
In exams use the working space and a pencil to try the execution