How do I determine if a value is within a range? - c

I am relatively new to C, I have to do it for school unfortunately and I am having issues with it at the easiest exercises.
Here I have to check if a number is in a certain interval, for example between 4 and 6. I made it like this.
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%s", i);
if (i>4 && i<6){
printf("%s Value is in first interval\n", i);
}
}
The scanf to enter the number and check if it is in the interval. But even if I enter a number that is part of it, for example 5, the printf doesn't do anything. I tried also to add an else statement for numbers outside the interval, but also there the printf did not change anything.

It is because you have declared i variable as int and you are taking input as string so when it is checking condition it is getting null value in i variable and not able to enter if block check below code
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%d",&i);
if (i>4 && i<6){
printf("%d Value is in first interval\n", i);
}
}
try compiling your code without if condition i variable will return a null value

Related

Infinite for loop? ()in c

the value of i resets after it reachers 7
#include <stdio.h>
int main(){
char marks[10];
int i;
printf("enter the numbers:\n");
printf("-------------------\n");
for (i=0;i<10;i++)
{
printf("%d\n",i);
printf("element %d-",i);
scanf("%d", &marks[i]);
}
printf("\n all %d",marks);
printf("\n second %d\n",marks[1]);
return 0;
}
output
Problem is here:
scanf("%d", &marks[i]);
Specifier "%d" expects a pointer to int, not char. Usually it will write 4 bytes what is a typical size of int.
Therefore on 8th iteration the elements of marks at index from 7 to 10 are touched. However, marks[10] is outside of marks array (only indices 0-9) are valid. Undefined Behaviour is invoked and the program can do anything, from crashing to infinite looping or conjuring nasal deamons.
To fix the program change the type of marks to int:
int marks[10];
Note:
UB is invoked even on the first iteration because "%d" expects a pointer to int while type of &marks[0] is char*. This operation is undefined by C standard because int* and char* may differ in size and/or representation and/or alignment. However it is a unlikely case for modern CPUs.
You have declared the marks as a character array and tried to get input from user using %d which asks asks for an integer,
#include <stdio.h>
int main(){
int marks[10];
int i;
printf("enter the numbers:\n");
printf("-------------------\n");
for (i=0;i<10;i++)
{
printf("%d\n",i);
printf("element %d-",i);
scanf("%d", &marks[i]);
}
printf("\n all %d",marks);
printf("\n second %d\n",marks[1]);
return 0;
}
Also I didn't understand the use of all so I couldn't find a solution for it. If you want to print a specific number then you have to specify it like you have done it for second or if you want to display the total you want to add a furthermore code to calculate the sum of elements in the array.

Why does my program crash after entering an integer? - C

I'm extremely new to C coding and i'm wondering why this is crashing like this? After I input a value and press enter, my program instantly crashes. I remember learning there are times when you use a & with an array in the scanf line and sometimes you don't. So when I remove the & it crashes instantly. I'm not sure how to troubleshoot this problem and would appreciate help.
What i'm trying to accomplish:
"Write a program that asks the user to enter a sequence of integers terminated by 0 ( the last number is 0) and prints all the numbers entered on one line."
The program crashes before I can enter the other variables. I was not done coding but since it keeps crashing instantly I can't go further.
int main () {
int ru[1000];
int read;
int nums;
int counts;
printf("Enter integers, press 0 to end user input \n");
while (nums>0) {
scanf("%d",&ru[nums]);
if (nums==0)
printf("%d ", ru[nums]);
}
system("pause>nul");
return 0;
}
As noted by several people already, you don't ever assign a value to nums at any point in your code, but make use of it in several places.
You should populate nums and whilst it's more than zero (this should probably be not equal to zero if you want to also include negative integers), store it's value into your array. You can track where you are in the array using another variable (I've picked the read one that you'd already declared), making sure that it is first initialised to 0.
Once the while loop is terminated, either by nums being zero or you filling up the array, you can then print out the numbers you've collected.
int main (void) {
int ru[1000];
int read=0;
int nums;
int counts;
printf("Enter integers, press 0 to end user input \n");
scanf("%d",&nums);
while ((nums>0)&&(read<1000)) {
ru[read++]=nums;
scanf("%d",&nums);
}
for(counts=0;counts<read;counts++) {
printf("%d ",ru[counts]);
}
printf("\n");
system("pause>nul");
return 0;
}
The scanf() you are using as the following prototype:
int scanf(const char *format, ...);
you should give the de pointer to the buffer variable as a parameter but your are giving a pointer to an array (pointer to a pointer).:
scanf("%d",&ru[nums]);
A solution to your problem might be:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int ru[1000];
int i = 0;
printf("Enter integers, press 0 to end user input \n");
do
{
scanf("%d",&ru[i]);
}while (ru[i]!= 0 && i++ < 1000);
for(i = 0; ru[i] != 0 ; i++)
printf("%d ", ru[i]);
return EXIT_SUCCESS;
}

Unable to convert values from lowercase to uppercase using toupper

i'm attempting to create a program that asks the user to firstly enter the amount of values they would like to convert from lowercase to uppercase. The for loop then assigns each value into an array. The array then goes through another for loop to convert the values into uppercase using LowerToUpper function.
When i go to run the program, it will take in values and then start doubling them on the command window, and will cease when you have completed entering the values, rather than printf the results. Could you please explain why. Thank you in advance
#include<stdio.h>
#include <string.h>
#include <ctype.h>
void LowerToUpper(char* array)
{
toupper(*array);
}
int main(void)
{
int i, amount;
printf("How many values?\n");
scanf("%d", &amount);
char *d;
char array1 [amount];
printf("Please enter the values\n");
for(i=0; i<amount; i++)
{
scanf("%c", &array1[i]);
}
for(i=0; i<amount; i++)
{
d=&array1[i];
LowerToUpper(d);
scanf("%c", &array1[i]);
printf("%c", array1[i]);
}
return 0;
}
You are not using toupper() properly. The function returns the converted value in case of success. You need to make use of the returned value. The supplied argument is not changed.
That said, the program structure is unnecessarily complicated. you can simplify it like
for(i=0; i<amount; i++)
{
int result = toupper (array1[i]);
if (result != array1[i]) printf("%c", result); //just checkin', if converted
}
That said, you have many other issue which you don't see at this moment, like
scanf("%c", &array1[i]);
this will, to your surprise, only ask you for half the number of inputs. Why? You forgot to ignore the newline entered by RETURN key.
Then, you did not check for the success of scanf("%d", &amount); call. In case, the scanning fails, you'll end up with undefined behavior.
The second scanf() inside the last for loop is probably something you don;t want, it's useless, at best.
Change this:
toupper(*array);
to this:
*array = toupper(*array);
since toupper ref's mentions:
Return Value
The uppercase equivalent to c (*array in your case), if such value exists, or c (unchanged) otherwise. The value is returned as an int value that
can be implicitly casted to char.
PS: Defining a function LowerToUpper() for this operation (one line of code) is an overkill, and you could call that one line of code inside main() instead (I mean the body of the function to be moved into main()).

How to count loop execution with local variables and user defined function? C Language

Please help as I am a beginner here. In this program, main asks the user to enter an integer. If the integer is positive, the loop will continue, and ask to enter another integer. The user-defined function named "myfunction" displays in output how many times the do-while loop has been called. I used variable loopCount++ to increment this count each time. This program works correctly, but I have just been given a new challenge: do this without using any global variables. Turns out I have one global var... int loopCount. I have no idea how to accomplish this using local variables but I sure do want to know. PLEASE help!
#include <stdio.h>
#include <stdlib.h>
int loopCount;
int main()
{
int number;
do{
printf("Enter a number: ");
scanf("%i", &number);
loopCount++;
myfunction();
}while(number > 0);
exit(0);
}
int myfunction(){
printf("The loop has been called %d times\n\n", loopCount);
}
Just make loopCount a variable that is local to main, and pass it to myfunction as an argument, like so:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
int loopCount = 1;
do{
printf("Enter a number: ");
scanf("%i", &number);
myfunction(loopCount++);
}while(number > 0);
exit(0);
}
int myfunction(count){
printf("The loop has been called %d times\n\n", count);
}
It's also probably worth pointing out that this code could use better error handling - if you're not sure what I mean, try typing a letter at the prompt (instead of a number).

failing to display array entered from keyboard in c

I'm new to c and I'm trying to write a c program that get 10 integer values entered from keyboard using scanf and then print them using printf but the result is not correct. Here is the code:
#include<stdio.h>
#include<conio.h>
main(){
int x[10];
printf("\n\n\t\t PRGRAM THAT CAPTURES AND PRINTS 10 SCORES");
for(int i=1;i<=10;i++){
printf("\n\tEnter Score %d", i);
scanf("%d",x);
}
printf("\n\t The entered scores are: %d",x[i]);
return(0);
}
the output given is a four digit number like 8731 yet I expect something like 1234567890. some help please
You need to make a new for loop to display the values, just like you do when reading them.
PS: format your code better, you'll thank it later.
PS2: try to avoid conio.h, it's not standard, and you don't even need it for your code.
PS3: also your code is wrong. Should be for(int i=0;i<10;i++). Arrays go from 0 to size-1, not from 1 to size. The C compiler will not warn you that i[10] is an invalid index for your array.
Try this:
#include<stdio.h>
#include<conio.h>
int main(){
int x[10];
printf("\n\n\t\t PRGRAM THAT CAPTURES AND PRINTS 10 SCORES");
for(int i=0;i<10;i++){ //Change 1
printf("\n\tEnter Score %d", i);
scanf("%d",&x[i]); //Change 2
}
//Change 3
for (int i=0; i<10; i++)
printf("\n\t The entered scores are: %d",x[i]);
return(0);
}
#include<stdio.h>
#include<conio.h>
The header <conio.h> is not standard. You get better portability if you don't use it. Anyway, your program doesn't use anything from it.
main(){
The function main() returns an int. Get into the habit of saying so explicitly (and you might also get into the habit of objectively specifying it takes no parameters).
int x[10];
printf("\n\n\t\t PRGRAM THAT CAPTURES AND PRINTS 10 SCORES");
For better working of printf() in all implementation, end each one with a '\n'. Otherwise the output might appear out of order.
for(int i=1;i<=10;i++){
printf("\n\tEnter Score %d", i);
scanf("%d",x);
Trying to always read to the same position in the array in a loop?
}
printf("\n\t The entered scores are: %d",x[i]);
The element x[i] does not exist. At this point in the code, i is larger than the largest legal array index.
return(0);
}

Resources