trying to find the smallest and largest value in the array, I'm getting the wrong small value. Why ?
biggerY=lowerY=arrY[0];
for(int loop2 = 1; loop2<10; loop2++)
{
if(arrY[loop2]>biggerY && arrY[loop2] != 0)
biggerY=arrY[loop2];
if(arrY[loop2]<lowerY && arrY[loop2] != 0)
lowerY=arrY[loop2];
}
my input :
1 2
2 1
The values on the right are saved in my array(arrY[10]).
expected biggerY: 2
expected lowerY : 1
my output for biggerY : 2
my output for lowery : 0 (but it should be 1)
thanks.
The problem with your code is that irrespective of the size of the arrY array, you run the loop till 10. You should run the loop till its size.Assuming the size of arrY is in the variable n
biggerY=lowerY=arrY[0];
for(int loop2 = 1; loop2<n; loop2++)
{
if(arrY[loop2]>biggerY && arrY[loop2] != 0)
biggerY=arrY[loop2];
if(arrY[loop2]<lowerY && arrY[loop2] != 0)
lowerY=arrY[loop2];
}
Related
for example, if I enter 12, I want to get 81 41 as the set bits in 12 are 1100
This is what I have for now, I do not think I am implementing the for loop correctly
#include <stdio.h>
void bin(unsigned n)
{
char list[6];
int x = 0, y = 1;
/* step 1 */
if (n > 1)
bin(n / 2);
/* step 2 */
list[x] = n % 2;
x++;
/*for(int i = 0; i < x; i++) {
printf("%d\n",list[i]);
}*/
for(int i = 0; i < 5; i++) {
if(list[i] == 1 && i == 5) {
printf("32%i",y);
}
if(list[i] == 1 && i == 4) {
printf("16%i",y);
}
if(list[i] == 1 && i == 3) {
printf("8%i",y);
}
if(list[i] == 1 && i == 2) {
printf("4%i",y);
}
if(list[i] == 1 && i == 1) {
printf("2%i",y);
}
if(list[i] == 1 && i == 0) {
printf("1%i",y);
}
}
}
I checked that I was correctly storing the bytes in the array, and it outputted correctly, but when I try to look for them one at a time in a loop, it seems to get stuck on the 32 bit integer, so for 12, it would print 321 321
This program has Undefined Behaviour from accessing uninitialized values of list. I'm going to refactor this code so its easier to talk about, but know this refactored code is still incorrect.
x is always 0. y is always 1. x++ has no effect. This function can be rewritten as:
void bin(unsigned n)
{
char list[6];
if (n > 1)
bin(n / 2);
list[0] = n % 2;
for (int i = 0; i < 5; i++) {
if (list[i] == 1) {
switch (i) {
case 5: printf("321"); break;
case 4: printf("161"); break;
case 3: printf("81"); break;
case 2: printf("41"); break;
case 1: printf("21"); break;
case 0: printf("11"); break;
}
}
}
}
There are some problems here.
Firstly, list is not shared between calls to bin, nor are any other variables.
In every call to bin, only list[0] is assigned a value - all others indices contain uninitialized values. You are (un)lucky in that these values are seemingly never 1.
With your example of 12 as the starting value:
When you initially call bin(12), what happens is:
bin(12) calls bin(6), bin(6) calls bin(3), bin(3) calls bin(1).
Starting from the end and working backwards, in bin(1):
n = 1, so list[0] = n % 2; assigns 1. The loop checks each element of list for the value 1, finds it when the index (i) equals 0, and prints 11.
This is repeated in bin(3), as 3 % 2 is also 1, and again this result is assigned to the first element of list. Again, we print 11.
In bin(6), 6 % 2 is 0. The loop finds no elements of list that equal 1. Nothing is printed.
And again, this is repeated in bin(12), as 12 % 2 is 0. Nothing is printed.
To reiterate, it is pure luck that this program appears to work. Accessing list[1] through list[4] (i < 5 ensures you never access the last element) in each function call is Undefined Behaviour. It is generally not worth reasoning about a program once UB has been invoked.
When dealing with bits, it would be a good time to use some bitwise operators.
Here is a program that more-or-less does what you have described.
It assumes 32-bit unsigned (consider using fixed width types from <stdint.h> to be more precise).
This program works by repeatedly shifting the bits of our initial value to the right b number of places and testing if the rightmost bit is set.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
unsigned num = argc > 1 ? atoi(argv[1]) : 42;
unsigned b = 32;
while (b--)
if ((num >> b) & 1)
printf("%u1 ", 1 << b);
putchar('\n');
}
$ ./a.out 12
81 41
I have a problem in the following part of my code in C:
// reading pairs of parameters from input file
while((read = fscanf(input_file, "%d %d\n", &type, &col)) > 0 ) {
// checking reading errors
if (read != 2 || (type < 0 && type > 4) || (col > 9)) {
printf("Invalid input file format\n");
return 2;
}
// putting tetromino corresponding to given parameters to the field
if (put_tetromino(field, type, col) != 0) {
// if result is not 0, the tetromnio is out of range. Returning error
printf("Tetromino is out of field\n");
return 2;
}
}
The input file looks like this:
5 0
3 9
2 9
2 4
..
In the above part of the code I want to check if the input file has the correct format.
I should have 2 columns: the first (type) has to be a value between 0 and 4 and the second (col) has to be a value between 0 and 9. If the input file contains a wrong format, for example:
9 8
4 9
2 5
..
I want to return 2. But the program doesn't return 2, it returns 0 at the end of the main-function.
The expression (type < 0 && type > 4) is always false - a number can't be both larger than four and less than zero. You should use an || instead of an && there:
if (read != 2 || type < 0 || type > 4 || col > 9) {
actually a number can not be at a time bigger than 4 and smaller than 0 . and also you need to check if col < 0. it should look like this if (read != 2 || (type < 0 || type > 4) || (col<0 || col > 9))
for your need you need to this.
// reading pairs of parameters from input file
while((read = fscanf(input_file, "%d %d\n", &type, &col)) > 0 ) {
// checking reading errors
if (read != 2 || (type < 0 || type > 4) || (col<0 || col > 9)) {
printf("Invalid input file format\n");
return 2;
}
// putting tetromino corresponding to given parameters to the field
if (put_tetromino(field, type, col) != 0) {
// if result is not 0, the tetromnio is out of range. Returning error
printf("Tetromino is out of field\n");
return 2;
}
}
Please explain to me why this code is wrong for the task and below I have explained all the four conditions -[][1]
#include stdio.h
int main()
{
int n;
scanf ("%d", &n); //taking input
if (n / 2 != 0)
{
printf ("Weird"); //checking first condition
}
else if (n % 2 == 0 && 2 <= n <= 5)
{ //checking second condition
printf ("Not Weird");
}
else if (n % 2 == 0 && 6 <= n <= 20)
{ //checking third condition
printf ("Weird");
}
else if (n % 2 == 0 && n > 20)
{ //checking fourth condition
printf ("Not Weird");
}
else
{
printf ("Error");
}
return 0;
}
this is the image for the question[1]: https://i.stack.imgur.com/OtY7o.png**
Testing for Odd or Even
n / 2 != 0 does not test whether n is odd. n/2 calculates the quotient that results from dividing n by 2 (rounding any fraction down). So 0/2 is 0, 1/2 is 0, 2/2 is 1, 3/2 is 1, 4/2 is 2, and so on. So n / 2 != 0 is true for all n other than −1, 0, and 1.
To test whether a number is odd, you can use n % 2 != 0. n%2 calculates the remainder from the division. If it is zero, n is even. If n is not zero, n is odd.
Using Else Efficiently
Once you have tested whether n is odd using n % 2 != 0, you do not have to test whether it is even in the else clauses. The else expressions and their statements will be evaluated only if the if expression is false, which happens (after the correction above) only when n is even. So we do not need to test again.
Testing For an Interval
In C, 2 <= n <= 5 does not test whether n is between 2 and 5. It is parsed as (2 <= n) <= 5. This is evaluated by comparing 2 to n, which produces 0 (if false) or 1 (if true). This result, 0 or 1, is then used in … <= 5. Since 0 and 1 are both less than or equal to 5, the result is always 1 (for true).
To test whether n is greater than or equal to 2 and less than or equal to 5, you must write this out explicitly: 2 <= n and n <= 5, which we join with the “and” operator, &&: 2 <= n && n <= 5.
Other Issues
The proper form for including stdio.h is #include <stdio.h>, not #include stdio.h.
A proper declaration for main is int main(void), not int main().
Corrected Program
A program with these issues corrected is:
#include <stdio.h>
int main(void)
{
int n;
scanf("%d", &n); //taking input
if (n % 2 != 0)
{
printf ("Weird"); //checking first condition
}
else if (2 <= n && n <= 5)
{ //checking second condition
printf ("Not Weird");
}
else if (6 <= n && n <= 20)
{ //checking third condition
printf ("Weird");
}
else if (n > 20)
{ //checking fourth condition
printf ("Not Weird");
}
else
{
printf ("Error");
}
return 0;
}
There is a typo in the line:
if (n / 2 != 0)
The compiler will not complain, but you will get unexpected results at run time.
Here you meant to check if the remainder of division by 2 is not equal to zero (i.e.: modulus operator), and not the division by 2. This line should be
if (n % 2 != 0)
Second thing: you can't tell C to compare values in ranges like this 2 >= n >= 4. You will have to split the comparison into 2 comparisons. This line:
else if (n % 2 == 0 && 2 <= n <= 5)
Should be:
else if (n % 2 == 0 && 2 <= n && n <= 5)
You will need to fix all the lines that have this comparison as well.
I have my code to run until the user inputs "0 0 0" to stop the program
but my program stops after one loop. I tried adding a print in the inner loop to see what the values were and maybe they were all getting set 0.
my example input
5 10 6
5 3 4 2 4
output
p = 4, s = 9, c = 6
p = 3, s = 6, c = 6
p = 2, s = 4, c = 6
p = 1, s = 0, c = 6
Scenario #1: MHR rides coaster #4, using the single rider line.
I can see that p, s, and c are not all 0 so I don't know why it breaks out of the outer loop when it should just go back to asking for the 3 user input values
#include <stdio.h>
#include <stdlib.h>
int main(){
int p,s,c,h,x=1,coaster;
while(p != 0 && s != 0 && c != 0){
//number of parties, single riders, capacity of ride
scanf("%d%d%d",&p,&s,&c);
//allocate memory
int* parties = malloc(sizeof(int)*p);
for(h=0;h<p;h++){
//get size of each party in line
scanf("%d",&parties[h]);
}
//find the faster line for each scenario
int t = 0;
while(p != 0 || s > 0){
coaster = c - parties[t];
s = s - coaster;
p--;
printf("p = %d, s = %d, c = %d\n",p,s,c);
if(p == 0 && s != 0){
printf("Scenario #%d: MHR rides coaster #%d, using the regular line.\n",x,t+1);
break;
}
if(s <= 0 && p != 0){
printf("Scenario #%d: MHR rides coaster #%d, using the single rider line.\n",x,t+1);
break;
}
if(s <= 0 && p == 0){
printf("Scenario #%d: MHR rides coaster #%d, using either line.\n",x,t+1);
break;
}
t++;
}
x++;
free(parties);
}
return 0;
}
The loop condition you are using is effectively: p not zero AND c not zero AND s not zero. So when s is zero, the condition is false and the loop exits.
The condition you are looking for is NOT (p is zero AND c is zero AND s is zero):
!(p == 0 && c == 0 && s == 0)
There is another bug in the program, you don't initialise p, c or s before checking their value.
Well, you have:
int p,s,c,h,x=1,coaster;
while(p != 0 && s != 0 && c != 0){
which is not good. You check for the values of p, s and c, but they are uninitialized variables!
if you want to quit if everything becomes zero, Change:
while(p != 0 && s != 0 && c != 0)
to:
while(!(p == 0 && s == 0 && c == 0))
Whenever I run this (part of a much larger file), when I get asked for inputs, if they are not numbers (letters or words) the code seems to loop and I'm not sure why.
while(rembox>=1){
printf("%c> ", p );
s=scanf("%d %d %c",&r , &k, &orin);
if (r = 5 || k =10){
*statement*
rembox --;
}
else{
rembox --;
continue;
}
At this line :
if (r = 5 || k =10){
You are assigning values 5 and 10 to r and k variables.
What you wanted to do :
if (r == 5 || k ==10){