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 3 years ago.
Improve this question
I was solving a problem on strings
Given a string S, write a program to title case every first letter of words in string.
Input:
The first line consists of an integer T i.e number of test cases. T testcases follow. The first and only line of each test case consists of a string S.
Output:
For each testcase, in a new line, print the required output.
Constraints:
1 <= T <= 100
1 <= |S| <= 1000
Example:
Input:
1
I love programming
Output:
I Love Programming
and for that I came up with this solution.
#include <stdio.h>
#include <string.h>
int main() {
//code
int t,flag;
scanf("%d", &t);
while(t--){
int n,i=0;
scanf("%d", &n);
char str[100];
scanf("%s", str);
while(i<n){
if (str[i]!= str[n-1-i]){
flag = 1;
printf("%d", flag);
break;
}
else{
flag = 0;
printf("%d", flag);
continue;
}
i++;
}
if(flag ==1 )
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
This code works fine when continue is removed, but when the above code is run, it prints 0 infinitly.
Can you help me where I'm going wrong?
Thanks in advance.
The "continue" here means "immediately perform the next iteration in the current loop, in this case the while loop".
In your code the line "i++" is therefor not executed and variable i never changes, thus causing an infinite 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 1 year ago.
Improve this question
Can someone please show or tell how to fix my code so that it can determine even numbers. Am new to for loops and C.
EDIT: please paste code here instead of image.
You could have just posted the code along with your question but man I could hardly understand your code so I'll make some silly assumptions about what you were trying to do.
1)
printf("even number is %d",);
Firstly I am doubly sure that this did not compile because of the hanging comma, number two removing the comma will either print rubbish or cause the entire program to crash because that %d is supposed to be accompanied by an integer argument for example:
printf("even number is %d", a);
It has to do with the way C handles variadic arguments.
2)
Instead of handling the input gotten from scanf you went on to check if i is an even number which makes no sense, of course the if statement there checks if it is an even number but then there's no need for asking for input.
3)
I suggest changing your code to this:
#include <stdio.h>
int main()
{
int a, i = 0; //remove one of the variables
printf("Enter 10 positive values\n");
while (i < 10)
{
printf("Enter a number:");
if(scanf("%d", &a) != 1) //ask for input first and check if scanf failed
{
printf("An error occured\n"); /*now scanf will push back the string or whatever it read back in the stdin stream
if it encounters a non integer value ('d' for example) and return 0 if this happens ther's something you can do about that but I'm not going to bother going into that
now such an error can happen because of other reasons do this research on your own*/
return 1;
}
if (a < 0) //check if a is positive
{
printf("%d is not positive please enter an even number\n", a); //print an error message if a is not positive
continue; //go back to the beginning of the loop
}
else
{
if ((a % 2) == 0)
{
printf("%d is an even number\n", a);
}
else
{
printf("%d is not an even number\n", a);
}
i++;
}
}
printf("Done!\n");
return 0;
}
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 3 years ago.
Improve this question
The program is supposed to find amicable pairs. The first input tells you how many numbers will follow and the program is supposed to figure out which of those numbers are amicable pairs. I don't know if the program is actually able to do that though, since I can't even get past the first for loop, which is literally just putting the elements that need to be checked into an array.
int main(int argc, char *argv[]) {
int numberOfNumbers, num;
scanf("%d", &numberOfNumbers);
int numbers[numberOfNumbers];
for (int i = 0; i < numberOfNumbers; i++) {
scanf("%d", &num);
numbers[i] = num;
}
I expect the program to move onto the calculation part of the code (which I didn't include) and produce some output, whether it's right or not, but instead after I've entered the last number it just acts as if it wants another input. At that point I could enter every digit of Graham's number and it still won't exit.
The first step of debugging a problem is validating you have the problem you're considering fixing.
for (int i = 0; i < numberOfNumbers; i++) {
scanf("%d", &num);
numbers[i] = num;
}
Is the loop that you are thinking of fixing.
printf("entering loop\n");
for (int i = 0; i < numberOfNumbers; i++) {
printf("i is %d, numberOfNumbers is %d\n", i, numberOfNumbers);
scanf("%d", &num);
numbers[i] = num;
}
printf("loop finished\n");
is the code you would would need to completely validate that your guess about the loop is correct (or wrong).
I hope this helps, even if it is not a direct answer. Your code looks good, but could be wrong based on a lot of items (including the user input).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.
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 5 years ago.
Improve this question
So basically I was creating a program to ask the user how many times they wanted to test the program.But I couldnt figure out the problem with my for loop. So:
If the user wants to test the program 3 times, it should ask for the value of a 3 times only and it should quit after.
This is my code as follows:
#include <stdio.h>
int main()
{
int test;
printf("How many times do you want to test the program?");
scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++) //I cant figure out whats going on with the for loop.
{
printf("Enter the value of a: \n");
scanf("%d", &test);
;
}
return 0;
}
The output should be:
"How many times do you want to test the program": 3
Enter the value of a : any numerical value
Enter the value of a: any numerical value
Enter the value of a: any numerical value
(exit)
In this section of your code:
scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++)
First, memory owned by test is populated with a value entered by user. (this is OK)
Next, you nullify that new value in memory by setting test to zero. (this is not OK)
finally the construction of your loop statement is incorrect.
In a correct version of your for loop, test should be a value used as a limit against which an index is tested as that index is incremented across a range of values, for example, from 0 to some positive value.
You probably intended:
scanf("%d", &test);
//test = 0; // Reinitializing the test from 0 (leave this out)
for(int i = 0; i <= test; i++)
{
...
Where a separate index value (i) is incremented and tested against the limit test.
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 just want to know what is the wrong with it, and how to fix it.
It looks like every thing is right, but when you run the .exe it crash every time
#include <stdio.h>
#include <stdlib.h>
// first list filling function:
void T_filling(int T[],int n){
int i;
for(i=1 ;i<=n ;i++){
printf("enter the number:",i+1);
scanf("%d",&T[i]);
}
}
//then the main algorithm:
int main()
{
int j,k,l;
int n,x;
// you can order up to 100 integer number
int T[100];
printf("This program is to order numbers decreasingly\n");
printf("how many numbers you want to order?\n");
// scanning the number of elements in the list
scanf(n);
//filling the list
T_filling(T[100],n);
//bubble sort Algorithm
for(j=1;j<=n-1;j++){
for(k=1;k<=n-j;k++){
if(T[k+1]>T[k]){
x=T[k];
T[k]=T[k+1];
T[k+1]=x;
}
}
}
for(l=1;l<=n;l++){
//printing the result on screen
printf("%d;",T[l]);
}
printf("\n");
system("pause");
return 0;
}
You are not using scanf and passing the array to function properly.
Please modify these lines in your code as follows and your program will work as expected.
scanf("%d", &n);
//filling the list
while(n > 100)
{
printf("Exceeding size, please re enter the size");
scanf("%d", &n);
}
T_filling(T,n);
Hope this helps.