Take an array of numbers and then display only the distinct number - c

In the below code I am taking an array to read 4 numbers and I need only to display the distinct number.
#include <stdio.h>
int main(){
int ch[3];
int s[3];
int count = 0;
int i;
int j;
for(i = 0; i < 4; i++){
scanf("%d", &s[i]);
for(i = 0; i < 4; i++){
ch[i] = s[i];
printf("ch= %d", ch[j]);
}
if(ch[i] == s[i]){
count = count + 1;
printf("%d =", count);
}
}
}
Somewhat I'm not getting the output, it is giving me a weird output.
Question:
What could be the cause of the strange output?

int ch[3];
int s[3];
This mean valid references to these arrays can be made from 0 to 2 , whereas your for loop is looping 4 times.
printf("ch= %d",ch[j]);
you mean ch[i] ?
what do you use "int j" for ?

Cant understand the question but i wrote a code for taking 4 inputs and giving the distinct number among them as an output(or the numbers which have only occurred once).
#include <stdio.h>
int main(){
int ch[4];
int s[4];
int i;
int j;
for(j=0;j<4;j++){
ch[j]=0;
}
for( i=0;i<4;i++){
scanf("%d",&s[i]);
}
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(s[i]==s[j]){
ch[i]++;
}
}
}
for(i=0;i<4;i++){
if(ch[i]==1){
printf("\n%d is the distinct number.", s[i]);
}
}
}

Related

change an integer to base 2 with arrays as output in C

#include <stdio.h>
#include<math.h>
int main(void){
int Num;
int i;
int N;
int x = 0;
int a[x];
scanf("%d", &Num);
N = Num;
for(i = 0; i < Num; i++)
{if (Num%2 == 0)
{a[i] = 0;}
else
{a[i] = 1;}
Num = Num/2;
}printf("%d in base 2 is %d", N, a[x]);
return 0;
}
program should convert an integer Num to base 2 eg 17 to 10001.
ideally using an array as the output
the remainder of the division of Num by 2 should be the last number in the output
then number is divided by 2 and the process repeats with the second output becoming the 2nd last output of the array
Sorry if this question is worded badly
Any help is appreciated
Thanks
#include <stdio.h>
#include <math.h>
int main(void){
int Num;
int N;
scanf("%d", &Num);
int x = floor(log2(Num)) +1;
int a[x];
N = Num;
for(size_t i=0;i<x;i++){
if(Num%2 == 0){
a[i]=0;
}else{
a[i]=1;
}
Num /= 2;
}
printf("%d in base 2 is ",N);
for(int i=x-1; i>=0;i--){
printf("%d",a[i]);
}
return 0;
}
I think the above code works fine for what you need (although it's not the best way to code this program).
In line 7 of your code, you defined x as 0; so, in line 8, the length of your array is 0 – and that's not what we need.
At the end, when you want to output the result, you only output the xth element of your array. Instead, we want to output every element that we stored. (I used i-- because, if I did not, the binary of Num would be reversed.)

C program shows different result in Linux and online compiler

I wrote a program in C to arrange the data in ascending order. When I compiled the code it showed no error but when it runs it shows a very different result than expected. However, when I ran the code in online C compiler it shows the correct result. I entered 5 different numbers 2 ,3 ,1 ,5 ,4.
Result in Linux: 0 1 2 3 4
Result in online compiler: 1 2 3 4 5
Why is this happening?
#include<stdio.h>
int * array(int x[],int l){
int i,j,k;
for(i=0;i<l;i++){
for(j=0;j<l;j++){
if(x[j]>x[j+1]){
k=x[j];
x[j]=x[j+1];
x[j+1]=k;
}
}
}
return x;
}
void main(){
int i,n;
int *b;
printf("enter n\n");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
scanf("%d",&a[i]);
b=array(a,n);
printf("the ascending order is: ");
for(i=0;i<n;i++){
fflush(stdout);
printf("%d\t",b[i]);
}
}
Your code accesses memory beyond your array:
for(j=0;j<l;j++){
if (x[j] > x[j + 1]) {
x[j] = x[j + 1];
x[j + 1] = k;
In your case, when n = 5, you allocate the array for 5 elements with indices 0,1,2,3,4. The latest element of the array is x[4]. But when your code runs and j == l-1, you try to compare and even modify the element x[5]. In fact, your program should crash as it tries to access the "unallocated" memory. But probably because of aligning, the "x[5]" addresses the allocated memory. And, probably, x[5] = 0 on your computer, and your algorithm uses this element as a part of the sorting process. So your function array() returns the array of [0,1,2,3,4,5] and then your main() prints first five elements of this array.
That's why you've got sorted elements [0,1,2,3,4] instead of [1,2,3,4,5].
BTW, the bubble algorithm can be optimized to not touch already sorted elements.
Also, remember the array doesn't copy to pass into the function, the array always passes by its address, so it is not needed to "return" the modified array.
So, the final code can look like:
#include <stdio.h>
void array(int x[], int l)
{
int i, j, k;
for (i = l; i > 1; i--) {
for (j = 1; j < i; j++) {
if (x[j - 1] > x[j]) {
k = x[j - 1];
x[j - 1] = x[j];
x[j] = k;
}
}
}
}
void main()
{
int i, n;
printf("enter n\n");
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++)
scanf("%d",&a[i]);
array(a, n);
printf("the ascending order is:");
for (i = 0; i < n; i++) {
printf(" %d", a[i]);
fflush(stdout);
}
printf("\n");
}
Of course, there are lots of things to be done in this code, like human-readable variables, formatting, further optimization. But I hope you can do it yourself.
You may find it easier to write programs to get their input from the command line instead of prompting for it. Using C11, you can allocate an array using the length provided by argc, and process the argv array directly:
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
if( argc == 1 ) {
errx(EXIT_FAILURE, "syntax: %s values...", argv[0]);
}
int a[argc - 1];
for( int i=0; i < argc-1; i++ ) {
if( 1 != sscanf(argv[i + 1], "%d", a + i) ) {
errx(EXIT_FAILURE, "could not scan '%s'", argv[i + 1]);
}
}
array(a, sizeof(a)/sizeof(a[0]));
printf("the ascending order is:");
for (i = 0; i < n; i++) {
printf(" %d", a[i]);
fflush(stdout);
}
printf("\n");
}

I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work?

I am trying to create an array and fill it with numbers from 1 to 10. Why it doesn't work? After filling it all the numbers should be printed.
#include <stdio.h>
int main() {
int i;
int number[10];
for(i=1; i<=10; i++)
{
printf("%d\n",number[i]);
}
printf("\n");
return 0;
}
In C this first index is 0. Therefore the code should use indexes 0 through to 9
I.e.
#include <stdio.h>
int main() {
int i;
int number[10];
for(i=0; i<10; i++)
{
number[i] = 1 + i;
printf("%d\n",number[i]);
}
printf("\n");
return 0;
}

C programming with arrays

This was the question
int num,square;
int array1[100];
int array2[100];
for(int i=0;i<100;i++){
printf("please enter the number %d:",i+1);
scanf("%d",&num);
if(num==-1){
break;
}
else
array1[i]=num;
}
for(int j=0;j<100;j++){
square=array1[j]*array1[j];
array2[j]=square;
}
for(int m=0;m<100;m++){
printf("number %d is %d\n",m,array2[m]);
}
this was the program i made but then the user might input only few int so i want to run the loop which prints the second array only the amount of tym the user inputted how can i do that?
Declare the first loop variable (i) outside the for.
This will remember the point at which the first loop exited.
Then use that for ending second loop instead of a hard-coded value i.e. 100.
int num,square;
int array1[100];
int array2[100];
int i=0;
for( i=0; i < 100; i++ )
{
printf("please enter the number %d:",i+1);
scanf("%d",&num);
if(num==-1)
{
break;
}
else
array1[i]=num;
}
for( int j=0; j < i; j++ )
{
square = array1[j] * array1[j];
array2[j] = square;
}
for(int m=0; m < i; m++)
{
printf("number %d is %d\n", m, array2[m]);
}
Your problem is when the user enters -1 and breaks the input loop. That will leave parts of array1 uninitialized. You are not allowed to access the uninitialized variables. Therefore you need to change the next two for-loops like:
for(int j=0;j<i;j++){
// ^ notice: i instead of 100
square=array1[j]*array1[j];
array2[j]=square;
}
for(int m=0;m<i;m++){
// ^ notice: i instead of 100
printf("number %d is %d\n",m,array2[m]);
}
This requires that the first loop is written like:
int i;
for(i=0;i<100;i++){
so that i is still valid when reaching the next loops.
In this way you avoid accessing uninitialized parts of array1 and you only print the same amount of values as the user did input (before -1).
Beside that... doing:
scanf("%d",&num);
is bad. You shall always check the value returned be scanf to make sure that it scanned the expected number of values, i.e.
if (scanf("%d",&num) != 1)
{
// Illegal input
....
Add error handling here
....
}
Just keep the number of entered numbers in a variable.
To store a product of two objects of the type int it is better to use type long long int.
Also it is not a good idea to use magic numbers as 100 throughout the code.
The program can look the following way
#include <stdio.h>
#define N 100
int main( void )
{
int array1[N];
long long int array2[N];
size_t n = 0;
for ( ; n < N; n++ )
{
int num;
printf("please enter the number %zu: ", n + 1);
if (scanf("%d", &num) != 1 || num == -1) break;
array1[n] = num;
}
for ( size_t i = 0; i < n; i++ )
{
long long int square = (long long int )array1[i] * array1[i];
array2[i] = square;
}
for ( size_t i = 0; i < n; i++ )
{
printf( "number %zu is %lld\n", i, array2[i]);
}
return 0;
}
I have updated your code.
int num,square;
int array1[100];
int array2[100];
int i=0;
for(i=0;i<100;i++){
printf("please enter the number %d:",i+1);
scanf("%d",&num);
if(num==-1){
break;
}
else
array1[i]=num;
}
/* This will only calculate square upto the numbers you have entered
I have changed the condition to <i
*/
for(int j=0;j < i;j++){
square=array1[j]*array1[j];
array2[j]=square;
}
/* This will only print upto the numbers you have entered
I have changed the condition to <i
*/
for(int m=0;m<i;m++){
printf("number %d is %d\n",m,array2[m]);
}

Find common value from two arrays

I am attempting to write a program which examines values in two arrays of different sizes and adds the common elements into a third array.
I am using the following code:
#include <stdio.h>
int main(void) {
// your code goes here
int A[5], B[8], i, j, s=1;
int* c;
c=(int*)malloc(s*sizeof(int));
for(i=0;i<5;i++)
{scanf("\t %d", &A[i]);}
printf("\n");
for(j=0;j=8;j++)
{scanf("\t %d", &B[j]);}
for(i=0, j=0;i<5,j<8;i++,j++)
{
if(A[i]==B[j])
{
c[i]=A[i];
s++;
printf("\n %d", c[i]);
c=realloc(c, s*sizeof(int));
break;
}
}
return 0;
}
but when I try to execute it, it is giving the problem that the time limit has been exceed. What is causing this problem? For compilation I am using the on-line compiler ideone.
Below is the code that would work.
Except for the mentioned problems in the for loops, assignment to array c was wrong.
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int A[5], B[8], i, j, s=1;
int* c;
c=(int*)malloc(s*sizeof(int));
for(i=0;i<5;i++) {scanf("\t %d", &A[i]);}
printf("\n");
for(j=0;j<8;j++) {scanf("\t %d", &B[j]);}
for(i=0;i<5;i++){
for(j=0;j<8;j++){
if(A[i]==B[j]){
c[s-1] = A[i]; // use s-1 as an index
printf("\n %d", c[s-1]);
s++;
c=(int*)realloc(c, s*sizeof(int));
}
}
}
return 0;
}
Hmmm...where to begin?
Are you only looking for characters that are the same AND in the same position? It LOOKS as though that is what you are trying to do, but as the array A and B have different dimensions, you are going to get into some trouble. And are you ONLY looking for the FIRST match? With the "break", you will stop after the first match.
Assuming you are looking for ALL element common to both lists -- in ANY position -- you will want code more like the following:
#include <stdio.h>
int main(void)
{
int A[5], B[8], C[8];
for (int i = 0; i < 5; i++)
scanf(" %d ", &A[i]);
printf("\n");
for (int i = 0; i < 8; i++)
scanf(" %d ", &B[i]);
int ci = 0;
for (int ai = 0; ai < 5; ai++)
{
for (int bi = 0; bi < 8; bi++)
{
if (A[ai] == B[bi])
{
C[ci] = A[ai];
ci++;
break;
}
}
}
C[ci] = 0;
printf("Common chars: %s\n", C);
return 0;
}
The key to what you want, I think, is the nested for loops, which iterate through EACH of the strings (A and B) separately, while looking for matching characters.

Resources