I want to write a bits seggregation code, but I wonder what can I do to increase the speed of the code. Can I get rid of some loops etc?
Aim of code is to seperate 1s and 0s of an array. 0s should be at left and 1s should be at right.
Here is my code:
#include <stdio.h>
int main() {
//code
int testCase;
scanf("%d\n", &testCase);
while(testCase>0) {
int n;
scanf("%d\n", &n);
int countzero = 0;
while(n>0) {
int i;
scanf("%d ", &i);
if(i==0){
countzero++;
}
}
for(int i=0; i<countzero; i++) {
printf("0");
}
for(int i=countzero; i<n ; i++) {
printf("1");
}
printf("\n");
}
return 0;
}
I wonder what can I do to increase the speed of the code. Can I get rid of some loops etc?
The time complexity of each test case will be O(n) regardless of what you do, since you have to read all the numbers before you know when you stop printing zeros.
Now, as for speeding up, you can indeed remove at least one loop. Hint: you don't need to count how many zeros there are, only how many ones.
Related
I'm new to C and need some help to understand how this piece of code works. I know that it reads the values that the user writes, puts them into an array, and then prints them out.
But I don't understand why I need two "counters" (i and j) to do this. Can someone help me to figure it out?
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
int j=0;
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
while (j < 5)
{
j++;
printf ("\n%d\n", A[j]);
}
}
Technically, what you have aren't two counters, but two loops. If you wanted, to, you could just reuse i for the second loop as well, by doing something like this:
while (i < 5)
i++;
printf("Enter your %d number\n", i);
scanf("%d", &A[i]);
}
i = 0;
while (i < 5)
{
i++;
printf ("\n%d\n", A[i]);
}
As for why you have two loops, the reason is simple. The first loop (using i in your code), reads the 5 integers into the array A. After the first loop concludes, your array A holds the 5 int values, which you could've used however you wanted. In your case, you want to print those values. So what you do is use a loop for looping over the array elements and printing the values to the screen, one by one.
You don't need it, you can simply reset the first and reuse it. However you must increment your index only after having using it otherwise you will overflow the limit of the array :
#include<stdio.h>
int main ()
{
int A[5];
int i=0;
while (i < 5) {
printf("Enter your %d number\n", i);
scanf("%d", &A[i]); // the last must be 4 not 5
i++; //<== increment here
}
i=0;
while (i < 5)
{
printf ("\n%d\n", A[i]); //idem
i++;
}
}
I am solving the problem that compares the array that is filled with numbers which are randomly generated by the computer and array that human asked to input numbers. I did pretty well on the first one but have a problem with stacking numbers in the human array.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// numbers that are randomly generated by the computer
int Array_comp (int (*comp))
{
int j,i;
for (j = 0; j < 3; )
{
i = rand();
if (i < 10)
{
comp[j]=i;
j++;
continue;
}
}
return 0;
}
int main(void)
{
srand((int)time(NULL));
int arr_comp[3]={};
int arr_hum[3]={};
Array_comp (arr_comp);
// enter three numbers
for (int i = 0; i < 3; i++)
{
printf("number %d: ", i+1);
scanf("%d ",&arr_hum[i]);
}
// print input numbers
for (int i = 0; i < 3; i++)
{
printf("%d ",arr_hum[i]);
}
return 0;
}
these are my code for this problem. For me, it seems like there is no problem with stacking numbers in the human array.
However, the result is different from my thought.
this is the result of the code. My initial intention is to stacking numbers something like
number 1:1
number 2:2
number 3:3
and printed results different from this. I have no idea why this happens.
The problem you have is that trailing extra space in the scanf() format specifier. It needs to match additional whitespace(s) in order to get the scanning done.
scanf("%d ",&arr_hum[i]);
change to
scanf("%d",&arr_hum[i]);
This code is meant to find intersection and union of two unsorted arrays which may contain duplicates.
The union works fine, but when trying to access intersection array memory, I get garbage results.
I can't find out where the problem is, tried to debug it but it didn't help me much either.
note: remember[] is an array of flags that saves indices of intersection elements in two arrays.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,n1,n2,j;
int user1[20];
int unionSize;
int intersectionArraySize;
printf("enter array1 size::\n");
scanf("%d",&n1);
printf("array1: ");
for(i=0; i<n1; i++)
{
scanf("%d",&user1[i]);
if (i>0) if(user1[i]==user1[i-1]) user1[i-1]=user1[i];
}
int user2[20];
printf("enter array2 size::\n");
scanf("%d",&n2);
for(i=0; i<n2; i++)
{
scanf("%d",&user2[i]);
if (i>0) if(user2[i]==user2[i-1]) user2[i-1]=user2[i];
}
int unionArray[20];
int remember[20]= {0}; // save index of common elements btn 2 arrays
int intersectionArray[20]={0};
intersectionArraySize=0;
unionSize=n1;
int index=0;
for(i=0; i<n1; i++)
{
for(j=0; j<n2; j++)
{
if (user1[i]==user2[j])
{
remember[j]=1;
intersectionArray[index]==user1[i];
intersectionArraySize++;
index++;
}
else unionArray[i]=user1[i];
}
}
for(i=0; i<n2; i++)
{
if(remember[i]!=1)
{
unionArray[n1]=user2[i];
n1++;
unionSize++;
}
}
printf("Union: ");
for (i=0; i<unionSize; i++)
{
if(i==unionSize-1) printf("%d\n",unionArray[i]);
else printf("%d ,",unionArray[i]);
}
printf("intersection: ");
for (i=0; i<intersectionArraySize; i++)
{
if(i==intersectionArraySize-1) printf("%d\n",intersectionArray[i]);
else printf("%d ,",intersectionArray[i]);
}
return 0;
}
Here's the problem:
intersectionArray[index]==user1[i];
It should be:
intersectionArray[index]=user1[i];
General comments on your code:
It's pretty bad designed. For instance, unless you have a very good reason, do not calculate both at the same time. Have one block that does intersection and only that. Same for union.
You declare the arrays with a fixed size, which is a bit weird since you ask for the sizes. Not a very big deal, but be aware of dangers if you enter a size over 20.
What's worse is that unionArray has a size of 20. It should be 40.
index and intersectionArraySize is practically the same variable. Get rid of one.
This looks messy:
for (i=0; i<unionSize; i++) {
if(i==unionSize-1) printf("%d\n",unionArray[i]);
else printf("%d ,",unionArray[i]);
}
Do like this:
for (i=0; i<unionSize-2; i++) {
printf("%d ,",unionArray[i]);
}
printf("%d\n", unionArray[unionSize-1]);
Also not a very big deal, but why do a conditional check on every single element when you know that it is the last that should be treated specially?
I've made an attempt to create the above program,but there's some error.Here's my program
#include <stdio.h>
#include <conio.h>
void main()
{
int a[20],o[20],e[20],c[40],i,j,k,l,n;
printf("enter size of array");
scanf("%d",&n);
printf("enter array");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
for(k=1;k<=(n-j);k++)
{
if(a[i]%2==0)
{
o[j]=a[i];
break;
}
else
{
e[k]=a[i];
}
}
}
}
for(i=1;i<=j;i++)
{
c[l]=o[i];
}
for(i=1;i<=k;i++)
{
c[l+k]=e[k];
}
printf("The new array is");
for(l=1;l<=(j+k);l++)
{
printf(" %d ",c[l]);
}
getch();
}
Can anybody please help me rectify the error in the above program?Can someone also give me a few tips on how to become a good C programmer?
The problem is probably because you never initialize the variable l.
Using an uninitialized variables yields in undefined behaviour (google that).
Also your array indexes start at 1 as for example here:
for (i = 1; i <= n; i++)
In C array indexes start at 0, so you should write this:
for (i = 0; i < n; i++)
This appies to all your other for loops.
There may be other problems though.
Your program looks overly complicated
Using variable names such as odd instead of o and even instead of e would make your program much easier to read.
Your program is poorly formatted. Correct formatting is essential to readbility
Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}
Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++