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;
}
Related
If this has any error, kindly mention it. Because I may figure out some future consequences
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5,5,4,3,4,5},count[10]={0},i;
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
int x;
x=a[i];
count[x]=count[x]+1;
}
for(i=0;i<sizeof(count)/sizeof(count[0]);i++)
{
if(count[i]!=0)
{
printf("\n %d:%d",i,count[i]);
}
}
}
Generally? No. In this specific case? Maybe.
Instead of sizeof(a)/sizeof(a[0]), use a macro to get the array size.
Dont declare/initialize a loop variable and two arrays in one line.
You will get out of bounds issues as soon as a contains a number that is bigger than the length of count - 1 or smaller than 0.
I would do something like the following:
#include <stdio.h>
#include <string.h>
#define ARRAY_COUNT(array) (sizeof(array)/sizeof(array[0]))
void GetCountOfNumberInArray(int intArray[],
unsigned int intArraySize,
int numbersToCount[],
unsigned int numberCount[],
unsigned int numbersToCountSize){
memset(numberCount, 0, numbersToCountSize * sizeof(numbersToCountSize));
unsigned int count = 0;
for(unsigned int i = 0; i < intArraySize; i++){
for(int j = 0; j < numbersToCountSize; j++){
if(numbersToCount[j] == intArray[i]){
numberCount[j]++;
break;
}
}
}
}
int main(int argc, char *argv[]){
int intArray[] = {1,2,3,4,5,5,4,3,2,1};
int numbersToCount[] = {1,2,3,4,5,6,7,8,9,10};
unsigned int numberCount[ARRAY_COUNT(numbersToCount)];
GetCountOfNumberInArray(intArray,
ARRAY_COUNT(intArray),
numbersToCount,
numberCount,
ARRAY_COUNT(numbersToCount));
for(unsigned int i = 0; i < ARRAY_COUNT(numbersToCount); i++){
printf("number %i appears %u times in the array\n", numbersToCount[i], numberCount[i]);
}
}
That way you get a universal function to count a set of numbers in an array that still works similar to your original solution.
For a school assignment, I have been tasked with making a simple codebreaker game using the functions and prototypes provided. You should be able to input the length and difficulty of the code, and type in numbers to make your guess. The part I'm having trouble with is determining the Perfect and Imperfect matches. The program needs to tell the player how many perfect matches (correct number in the correct place) their guess had, and then reveal the perfect matches. The latter I have correct, but the former is only returning with 56 perfect matches when I test it. And I don't really know where to start with the Imperfect matches section, and how I can get it to skip perfect matches. Is there any way I can do this without strings?
Keep in mind, this is a section of the code, not the entire program. It's in this section that things are breaking.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void GetGuess(int guess[], int length) {
int i;
printf("\n\nPlease input your guesses.\n");
for(i = 0; i < length; i++){
scanf("%d", &guess[i]);
}
return;
}
void ProcessGuess(int hidelist[], int showlist[], int length, int guess[]) {
int i;
int perfectsum, imperfectsum;
for(i = 0; i < length; i++){
if(guess[i] == hidelist[i]){
PerfectMatches(hidelist, guess, length);
showlist[i] = guess[i];
}
else{
ImperfectMatches(hidelist, guess, length);
}
}
printf("You have %d perfect matches and %d imperfect matches!\n\n", perfectsum, imperfectsum);
return;
}
int PerfectMatches(int hidelist[], int guess[], int length) {
int i;
int perfectsum = 0;
for(i = 0; i < length; i++){
if(guess[i] == hidelist[i]){
perfectsum++;
}
}
return perfectsum;
}
int ImperfectMatches(int hidelist[], int guess[], int length) {
int i, j;
int imperfectsum = 0;
for(i = 0; i < length; i++){
for(j = 0; j < length; j++){
if(i != j){
if(hidelist[i] == guess[j]){
imperfectsum++;
break;
}
}
}
}
return imperfectsum;
}
void copyArray(int dest[], int source[], int length) {
}
Firstly in your function ProcessGuess you didn't get the return of the PerfectMatches and ImperfectMatches functions. So your perfectsum and imperfectsum aren't correct.
Then concerning the Imperfect Matches section you can use the PerfectMatches function but using != instead of == (if I understood the rules correctly)
So basically what my c program needs to do is find out if the first digit of a number is odd, and do so for every element of a 12- element array. Also I need to make the program in the manner that finding out if the first element of a single number is odd needs to be written in a special function outside of main(). This program is really easy to make only in main() but as far as I know you need to use pointers for an array if you want to do something with it outside of main() and I'm not really good at that tbh. So any help would do me a big favour I guess.
This is what I've done so far:
#include <stdio.h>
int function(int p[], int n)
{
int i;
int x;
for (i = 0; i < n; i++)
{
while (p[i] >= 10)
;
{
p[i] /= 10;
}
if (p[i] % 2 != 0)
{
x++;
}
}
return x;
}
int main()
{
int n = 12;
int array[n];
int i;
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
int r;
r = function(array[n], n);
printf("%d", r);
return 0;
}
And this is what my apparent errors are:
main.c:31:22: warning:
passing argument 1 of ‘function’ makes pointer from integer without a cast [-Wint-conversion]
main.c:3:9: note: expected ‘int *’ but argument is of type ‘int’
So yeah as I said, any help would do good. Also keep in mind that I'm in the first year of the first semester of college and that we can't really use anything outside of <stdio.h> or <stdlib.h> to write our code.
No pointers needed, unless you want to modify the array passed.
Some issues:
while(p[i] >= 10);{
p[i] /= 10;
}
The above code runs in an infinite loop, after which it runs p[i] /= 10; once.
Most C programmers have issues with missing ;. You have the opposite problem: A ; where it shouldn't be.
To put it simply, that intruding ; tells the compiler that the while loop doesn't run any code, so the compiler actually thinks your code means this:
while(p[i] >= 10) {
// Do nothing
}
p[i] /= 10;
int r;
r = function(array[n], n);
printf("%d", r);
That r variable is pointless. Unless you don't want to directly pass the return value from function
If the value of n is 12 then array[n] is the 12th element of array. Which doesn't exist because array only has elements 0-11
This is how I would write the code if I wanted to pass the whole array to my function:
printf("%d", function(array, n));
Here is a working version of the code you want
#include <stdio.h>
int count_odd_fdigits();
int main() {
const int ARRAY_SIZE = 12;
int array[ARRAY_SIZE];
int i;
for (i=0; i < ARRAY_SIZE; i++) {
scanf("%d", &array[i]);
}
printf("Numbers with an odd first digit: %d", count_odd_fdigits(array, ARRAY_SIZE));
return(0);
}
int count_odd_fdigits(int numbers[], int limit) {
int i;
int count = 0;
for (i=0; i < limit; ++i) {
while (numbers[i]/10 > 0)
numbers[i] /= 10;
if (numbers[i] % 2 != 0)
++count;
}
return(count);
}
(Run it online: https://onlinegdb.com/yEPr_mYgna)
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;
}
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;
}