void f() {
for (i == 0; i < 6 && i++, i <10; i++)
{
printf("%d", i);
}
}
int main()
{
f();
return 0;
}
Codes Picture
Output
Any tips on how to debug for loops on paper without a computer
In the code, by saying i == 0 (comparison operator) instead of i = 0 (assignment operator), in the successive use, you're using an uninitialized local variable, which has indeterminate value.
That said, the correct version should look like
#include <stdio.h> //header file is needed
void f() {
for (int i = 0; i < 6 && i++, i <10; i++) // define types
{
printf("%d", i);
}
}
int main(void) // correct signature
{
f(); // indent, optional but good
return 0;
}
Related
I have to convert a letter to binary number. All work but with one problem - I don`t understand why after my binary number it still prints some other numbers... Can someone help, please?
Here is my code. Thank you in advance!
#include <stdio.h>
#include <stdbool.h>
void convert(const char char, bool bits[8]) {
char c = char;
for (int j = 7; j+1 > 0; j--){
if(c>=(1<<j)){
c=c-(1<<j);
printf("1");
}else{
printf("0");
}
}
//here start to prints other numbers
printf("\n");
printf("\n");
}
int main(){
bool bits1[8];
encode_char('A', bits1);
for(int i = 0; i < 8; i++)
{
printf("%d", bits1[i]);
}
printf("\n");
return0;
}
There are 3 problems making your sample code unable to compile:
You tried to declare the first argument to your function as const char char, but char is a type and is not valid variable name.
You tried to call encode_char in main, but you defined convert
return0 should be return 0
After fixing those, there will still be a problem with garbage values.
Because even though you passed bits, the function never does anything with it (so it remains with garbage values).
So your printf("%d", bits1[i]); will be just a bunch of "random" numbers. The extra numbers are not from what you marked with //here....
Here is an example that assigns useful values to bits:
#include <stdio.h>
#include <stdbool.h>
void encode_char(const char input_char, bool bits[8]) {
char c = input_char;
for (int j = 7; j >= 0; j--){
if(c>=(1<<j)){
c=c-(1<<j);
bits[7-j] = 1;
}else{
bits[7-j] = 0;
}
}
}
int main(){
bool bits1[8];
encode_char('A', bits1);
for(int i = 0; i < 8; i++)
{
printf("%d", bits1[i]);
}
printf("\n");
return 0;
}
In my code I often have a for loop for doing a single operation n times. E.g:
// Wait for settle
int delayLoop = 0;
int count = 0;
for(delayLoop = 0; delayLoop < count; delayLoop++) {
__NOP(); // operation to do
}
At first I wanted to make this as an function....but then I realized I do not know how to pass in the operations as a function argument.
In the above example the __NOP() is itself a macro that expands to:
__ASM volatile ("nop")
So how can I come up with a macro that I can call like this:
DO_LOOP(10, __NOP)
and what if I need to do more operations? e.g.
DO_LOOP(8, __NOP, myFunction(arg1))
that would expand to:
for(int i = 0; i < 8; i++) {
__NOP;
myFunction(arg1);
}
#define DO_LOOP(x, ...) for (int i = 0; i < x; ++i) { __VA_ARGS__; }
void f1() { printf("test\n"); }
void f2() { printf("a\n"); }
int main()
{
DO_LOOP(10, f1(), f2());
return 0;
}
gcc -E test.c:
void f1() { printf("test\n"); }
void f2() { printf("a\n"); }
int main()
{
for (int i = 0; i < 10; ++i) { f1(), f2(); };
return 0;
}
This doesn't work with inline assembly though. You can do something like:
#define DO2(x, a, b) for (int i = 0; i < x; ++i) { a; b;}
and use:
DO2(10, __NOP(), f1());
I am new to functions and right now I am trying to understand them so please go easy on me if you see some "noob" mistakes.I would really appreciate some help with this program:
#include <stdio.h>
#include <stdlib.h>
int check(int a[],int n,int i )
{
int j;
for (j=0; j<n ; j++)
{
if(a[i]==j*j)
return 1;
else
return 0;
}
}
int main()
{
int n,a[100],i;
printf("\nThe size:\n");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\na[%d]=",i);
scanf("%d",&a[i]);
if(check(a,n,i)==1)
printf("%d is a perfect square\n",a[i]);
else
printf("%d is not a perfect square\n",a[i]);
}
return 0;
}
I succeeded in making it run but something isn't right no matter the input (1,4,5,9...) it will always print:" is not a perfect square "
What you need is to pass only a number to the function instead. To do that, you can simply do:
int check(int value, int n)
{
for (int j=0; j<n ; j++)
if(value==j*j)
return 1;
return 0;
}
and call the function like this:
check(a[i], n)
Now, you are not passing the whole array, but only a number, i.e. the i-th number of it.
At this point, you have a function to check one number. Think what you need to do, to check a collection of numbers. How would you apply that check() to every number of your vector?
PS: An important and fundamental note is that you should indent your code, since it makes everything much more readable (for example, curly brackets get aligned).
Appendix:
You could make your initial function work like this:
int check(int a[],int n,int i )
{
int j;
for (j=0; j<n ; j++)
{
if(a[i]==j*j)
return 1;
}
return 0;
}
so that you don't stop looping over when the condition is not mean, but will continue checking the other entries as well.
However, I strongly recommend using my suggestion above.
Write a function to check if its parameter (positive integer) is a perfect square
What is needed is:
bool check_if_perfect_square(unsigned n);
Simple find the integer square root. Integer square root routines are not too hard to code. Maybe:
#include <stdbool.h>
// Square root of t round toward 0
unsigned uisqrt(unsigned t) {
unsigned s, b;
for (b = 0, s = t; b++, s >>= 1) {
;
}
s = 1u << (b >> 1);
if (b & 1) {
s += s >> 1;
}
do {
b = t / s;
s = (s + b) >> 1;
} while (b < s);
return s;
}
If you do not like that advanced approach, code could slowly iterate. No need to iterate to i<n, but to i <= n/i.
unsigned uisqrt(unsigned n) {
unsigned i = 0;
if (n > 0) {
for (i = 1; i <= n/i; i++) {
;
}
i--;
}
return i;
}
Then the check is easily
#include <stdbool.h>
bool check_if_perfect_square(unsigned n) {
unsigned sr = uisqrt(n);
return sr*sr == n);
}
Then apply this function to a vector of positive integers
Armed with a check_if_perfect_square(), simply iterate over the array.
#include <stddef.h>
#include <stdio.h>
void square_root_test_array(unsigned *a, size_t array_length) {
for (size_t i = 0; i<array_length; i++) {
if (check_if_perfect_square(a[i])) {
printf("%u is a perfect square\n",a[i]);
} else {
printf("%d is not a perfect square\n",a[i]);
}
}
}
Sample use
int main() {
printf("\nThe size:\n");
unsigned n = 0;
scanf("%u",&n);
unsigned a[n];
for(unsigned i=0; i<n; i++) {
printf("a[%u] = ",i);
scanf("%d",&a[i]);
}
// Now test array
square_root_test_array(a, n);
return 0;
}
I am a beginner in programming. What is a segmentation fault and how to remove that on the following program?
The following is a compare the triplets problem asked in hackerrank. I receive a segmentation fault when attempting to run the program.
int main() {
int a, b, c;
c = points(a, b);
printf("%d", c);
return 0;
}
int points(int a[10], int b[10]) {
int p = 0, q = 0;
for (int i = 0; i < 3; i++) {
printf("%d", a[i]);
scanf("%d", & a[i]);
}
for (int j = 0; j < 3; j++) {
printf("%d", b[j]);
scanf("%d", & b[j]);
}
for (int k = 0; k < 3; k++) {
if (a[k] > b[k]) {
++p;
return p;
} else {
if (a[k] = b[k])
++q;
return q;
}
}
return 0;
}
int a,b,c;
c=points(a,b);
You are passing arguments to function without setting theirs value. Remember that in C, you are passing arguments to function by value.
int points(int a[10],int b[10])
This function expects two arrays of ten integers. You are passing only one integer for each.
You should also read about passing array arguments to function (if that was your intention). In C, You can't pass whole array to function unless it is wraped into a struct. You should only pass address of the first array element and in some way define size. For example:
#define ARRAY_SIZE 10
int foo(int tab[])
{
int i;
for (i = 0; i < ARRAY_SIZE; i++)
tab[i] = do_stuff();
}
int main(void)
{
int tab[ARRAY_SIZE];
foo(tab);
}
You can also not define ARRAY_SIZE macro, but pass another argument to a function which will define the size of passed array.
For some strange reason every time I compile my project I get this strange error of multiple definitions even though this is the only place I define my function. I use code blocks as my IDE and GCC as the compiler. Here is the code:
#include <stdio.h>
#include <test.h>
int checkForSquare(int num){
int squared[10001];
squared[num] = num * num;
for(int i = 0; i <= 10000; i++){
if(squared[i] == num){
return 1;
break;
}
}
return 0;
}
int main(){
for(int x = 0;x <=10000;x++){
if(checkForSquare(x))
printf( "%d" , x );
else
printf("Not Square");
}
return 0;
}
First, you can check the discussion below:
How to prevent multiple definitions in C?
Second, please properly align your code at least before you post your question.
Third, I think your checkForSquare() should look like this:
int checkForSquare(int num){
static long squared[10001];
squared[num] = num * num;
for(int i = 1; i < num; ++i){
if(squared[i] == num){
return 1;
}
}
return 0;
}