I want to write a C program to adjust the carry in an integer array (i.e. convert the 2 digit number into a single-digit and add the carry to the next number).
For example -
Array - 6 12 3 15 7
Answer: 7 2 4 5 7
Here's my code:
#include<stdio.h>
int main(){
int array[6]={6,22,3,15,7};
int array2[2];
int i;
printf("%d\n",array[1]);
for(i=0;i<6;i++){
if(array[i]>10){
array2[i]=array[i];
printf("Value at %d element of array is: %d \n",i,array2[i]);
}
}
return 0;
}
So far I have been able to write a program that just finds out double-digit numbers in the array.
I'm relatively new to C and don't know much about how to perform arithmetic operations in arrays.
Help me please!!
since we are adding the carry to previous element, we should start in reverse order.
#include <stdio.h>
int main()
{
int i;
int a[5] = {6,22,3,15,7};
for(i=4;i>0;i--)
{
if(a[i]>9)
{
int rem = a[i]%10;
int carry = a[i]/10;
a[i] = rem;
a[i-1] = a[i-1] + carry;
}
}
if(a[0]>9)
a[0] = a[0]%10;
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Related
Given a list of digits, for example: <1 6 3 2 5>
How can you turn it into an integer?
In this case, 16325.
I was able do the reverse operation, turning an integer into a list of digits, but im stuck with this one...
Here is a short program that takes an integer array and multiplies it out into an integer:
#include <stdio.h>
#include <math.h>
int main()
{
int output = 0;
int numbers[5] = {1,2,3,4,5};
int size = sizeof numbers / sizeof numbers[0];
for(int i = size; i > 0; i--)
{
output = output + numbers[size-i] * pow(10,i-1);
printf("%d\n",output);
}
return 0;
}
First, it finds the length of the array. Then it needs take each element and multiply it by a power of ten depending upon its location in the array, adding to output to get the total sum.
I am a beginner starting in C and am doing some exercises on codewars. The exercise requires me to take a decimal int, convert it into binary and output the number of 1s in the binary number. Below my incomplete code. I store the binary in int b and I want to output it into an array so that I can run a loop to search for the 1s and output the sum.
Thanks in advance!
#include <stddef.h>
#include <stdio.h>
//size_t countBits(unsigned value);
int countBits(int d);
int main() {
int numD = 1234;
int numB = countBits(numD);
printf("The number %d converted to binary is %d \n", numD, numB);
}
int countBits(int d) {
if (d < 2) {
return d;
} else {
int b = countBits(d / 2) * 10 + d % 2; //convert decimal into binary
int c;
int bArray[c];
}
Your function is almost correct:
you should define the argument type as unsigned to avoid problems with negative numbers
you should just return b in the else branch. Trying to use base 10 as an intermediary representation is useless and would fail for numbers larger than 1023.
Here is a corrected version:
int countBits(unsigned d) {
if (d < 2) {
return d;
} else {
return countBits(d / 2) + d % 2;
}
}
There are many more efficient ways to compute the number of bits in a word.
Check Sean Eron Anderson's Bit Twiddling Hacks for classic and advanced solutions.
You can make an array char as one of the replies said, for example:
#include <stdio.h>
#include <string.h>
int main(){
int count=0;
int n,bit;
char binary[50];
printf("Enter a binary: \n");
scanf("%s",binary);
n=strlen(binary);
for(int i=0;i<n;i++){
bit=binary[i]-'0';
if (bit==1){
count=count+1;
}
}
printf("Number of 1's: %d\n",count);
return 0;
}
This should count the number of 1's of a given binary.
Try something like this!
edit: I know that binary[i]-'0' might be confusing, if you don't understand that..take a look at this:
There are definitely 'smarter'/more compact ways to do this, but here is one way that will allow you to count bits of a bit larger numbers
#include <stdio.h>
int count_bits(int x)
{
char c_bin[33];
int count=0;
int mask=1;
for( int i =0; i < 32; i++){
if (x & mask ){
count=i+1;
c_bin[31-i]='1';
}
else{
c_bin[31-i]='0';
}
mask=mask*2;
}
c_bin[32]='\0';
printf("%d has %d bits\n",x,count);
printf("Binary x:%s\n",c_bin);
return count;
}
int main()
{
int c=count_bits(4);
return 0;
}
I have an array having 100 numbers say a[1,2,3,4,5,6....98,99,100]. I want to divide it into 25 groups with each group containing 4 elements and then subtract elements of other blocks with each element of one block. For example: if three blocks out of 25 are labelled as A,B,C and contain elements as:
A [1,2,3,4],
B[5,6,7,8] &
C[9,10,11,12]
then subtraction is to be done like this:
(A-B, A-C),
(B-A, B-C) &
(C-A, C-B)
i.e.
1-5,1-6,1-7,1-8,1-9,1-10,1-11,1-12; then
2-5,2-6,2-6,2-8,2-9,2-10,2-11,2-12; then
3-5,3-6,3-7,3-8,3-9,3-10,3-11,3-12; then
4-5,4-6,4-7,4-8,4-9,4-10,4-11,4-12;
THEN
5-1,5-2,5-3,5-4,5-9,5-10,5-11,5-12;
and like wise..
Can anyone help me in writing C program for this.
The code that I have written is partial and not doing the above task completely. Code is:
#include <stdio.h>
#include <conio.h>
void main()
{
int a[100]={1,2,3,4.....,98,99,100};
int i=0, j=0;
int x[100], y[100];
// considering only 12 numbers for the sake of simplicity
for (i=0;i<12;i++)
{
for(j=0;j<8;j++)
{
x[j] = a[i] - a[r+4];
}
y[i] = x[i];
}
}
#include <stdio.h>
int main(void) {
int N, i;
scanf("%d", &N);
int numArray[N]; // Define an array of four integers
// Get inputs for the array elements
for (i=0;i<N; i++) {
scanf("%d", &numArray[i]);
}
int sum = 0;
// Write here the logic to add these integers:
for (i=0;i<N;i++) sum += numArray[i];
printf("%d\n",sum); // Print the sum
return 0;
}
I am trying to solve two different C problems and would like some help and advice in order to better understand how C works and if I'm on the right track with these.
First problem is: To write a function that counts the number of times the value (x) appears among the first (n) elements of an array and returns that count as the frequency of x in theArray. So, an example would be if the array being passed contained the values {5, 7, 23, 8, 23, 67, 23}. And n was 7 and x was 23, then it would return a value of 3 since 23 occurs 3 times within the first 7 elements of the array.
Here is what I have so far:
#include <stdio.h>
#define SIZE 20 /* just for example - function should work with array of any size */
int frequency (int theArray[], int n, int x)
{
int i;
int count = 0;
for (i = 0; i < n; i++)
{
if (theArray[i] == x)
{
count = count++;
}
}
return (count);
}
int main(void)
{
/* hard code n and x just as examples */
int n = 12; /* look through first 12 items of array */
int x = 5; /* value to find */
int numberFrequency;
long int theArray[SIZE] = {5,2,3,4,5,6,1,2,10,5,10,12,6,8,7};
numberFrequency = frequency (theArray[SIZE], n, x);
printf ("%i", numberFrequency);
return 0;
}
Currently I'm getting a run time error message and believe it has something to do with the for loop function.
Second problem is: Write a function that raises an integer to a positive integer power. Have the function return a long int, which represents the results of calculating x to the nth power. Do not use the C pow library function and do not use recursion!
My code so far:
#include <stdio.h>
int x_to_the_n (int x, int n)
{
int i;
long int result = 1;
if (n == 0)
{
return(result);
}
else
{
for (i = 0; i < n ; ++i)
{
/* equation here - How can I make (x*x*x*x*x*x,etc...? */
result = x*(n*x);
}
}
return (result);
}
int main(void)
{
int x =4;
int n =5;
long int result;
result = x_to_the_n (x, n);
printf ("%i", result);
return 0;
}
I can't use recursion so that is out of the question. So, I thought the next best thing would be a for loop. But I'm a little stuck in how I would go about making a for loop do (xxx*x....) based on value of (n). Any help and advice would be appreciated!
In the first problem you give an element after the array as a parameter to your function.
You define a long int array, and pass it into a function expecting an int array.
long int theArray[SIZE] = {5,2,3,4,5,6,1,2,10,5,10,12,6,8,7};
should be
int theArray[SIZE] = {5,2,3,4,5,6,1,2,10,5,10,12,6,8,7};
Instead of this:
numberFrequency = frequency (theArray[SIZE], n, x);
try this:
numberFrequency = frequency (theArray, n, x);
And replace:
count = count++;
with:
count++;
For example take 123 and put it into an array where that is a[3] = {1, 2, 3}?
Without converting it to a string and iterating over it.
You can get the decimal digits of a number by using integer division and modulo.
//Pseudo code
int[MAX_SIZE] result;
int index = 0;
while (workingNumber > 0)
{
digit = workingNumber % 10;
result[index] = digit;
workingNumber = workingNumber / 10; //Must be integer division
index++;
}
#include <math.h>
...
int number = 5841;
int size = log10(number) + 1;
int arr[size];
int i = size;
while(i >= 0)
{
arr[--i] = number % 10;
number /= 10;
}
First, keep in mind that in C the only real difference between "array of char" and "string" is to be a string, you put a NUL-terminator at the end of the array of char.
Assuming you wanted (for example) to create an array of int (or long, or something else other than char), you'd typically take the remainder when dividing by 10 and convert it to a digit by adding '0'. Then divide the number by 10 and repeat until it's reduced to zero. That creates the numbers from least to most significant, so you normally deposit them at the end of the array and work backward toward the beginning.
#include <stdio.h>
#include <math.h>
#define LEN 3
int main(int argc,char* argv[])
{
int i = 123;
int a[LEN];
int digit;
int idx = log10(i);
do {
digit = i % 10;
i /= 10;
a[idx--] = digit;
} while (i != 0);
printf("a: { %d, %d, %d }\n", a[0], a[1], a[2]);
return 0;
}