Cicle FOR to generate two sequences of numbers - c

I use this code to create a number sequence, but what i want is increment a second sequence of numbers(each 1000 times 'i' increment, 'j' the second increment of 1)
for (i=0;i<5000;i++)
{
printf("/sample/%d/%d\n",j,i+1);
}
what i want is this:
/sample/0005/0005000

Please try this
#include <usual.h>
int main( )
{
int i = 0;
int j = 0;
int ticker = 1000;
for ( i = 0; i < 5001; i++ )
{
if ( i && i % ticker == 0 )
{
j++;
ticker += 1000;
printf( "\n sample i== %d j== %d ", i, j );
}
}
}

Related

Pyramid patterns using C

I need to do the following pattern using do-while, while or for.
I tried the following code but it prints the pattern only 1-5
I also tried to alter the n being 10 but then the spacing goes nuts.
#include <stdio.h>
int main(void)
{
int n = 5, i, j, num = 1, gap;
gap = n - 1;
for ( j = 1 ; j <= n ; j++ )
{
num = j;
for ( i = 1 ; i <= gap ; i++ )
printf(" ");
gap --;
for ( i = 1 ; i <= j ; i++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( i = 1 ; i < j ; i++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
Try replacing both occurences of:
printf("%d", num);
with
printf("%d", num % 10);
Now only the last digit will be shown.
After the change, for n set to 10 the program produces:
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210

How to skip certain numbers in a for-loop?

I started practicing a for loop in C and so far I understand the main principle behind it. But I can't figure out how to get the following output:
1 2 3 5 6 7 9 10 11 ...
I managed to print 1 to 12 with the following for loop but how can I skip 4 and 8 or how to skip any number in general?
for(int i = 1; i < 12; i++)
{
printf("%d", i);
}
Either you can use an if statement inside the body of the loop like
for ( int i = 1; i < 12; i++ )
{
if ( i % 4 != 0 )
{
printf( "%d ", i );
}
}
Or you can avoid the numbers that divisible by 4 in the third expression of the for loop like
for ( int i = 1; i < 12; i += ( i + 1 ) % 4 == 0 ? 2 : 1 )
{
printf( "%d ", i );
}
If you need to output the space character instead of a number divisible by 4 you can use an if-else statement inside the body of the loop. For example
for ( int i = 1; i < 12; i++ )
{
if ( i % 4 != 0 )
{
printf( "%d ", i );
}
else
{
putchar( ' ' );
}
}
the simplest solution would be to check with an if statement for any values that you don't want.
if you have a rule like not printing all numbers that are divisible by 4 you can make your if statement like this
if(i % 4 == 0)
{
//print
}
there is no way to do it specifically with the for loop expression.
if you want to do specific numbers you just add more conditions to the if statement:
for(int i = 1; i < 12; i++)
{
if(i != 11 && i != 5 && i%3 != 0)
{
printf("%d", i);
}
}
The best way to do this, especially for a large amount of irregular exceptions, is the following way.
#include <stdio.h>
int main(int argc, char *argv[])
{
int exceptionsv[] = {4, 8};
int exceptionsc = sizeof(exceptionsv)/sizeof(exceptionsv[0]);
for(int i = 1; i < 12; i++)
{
for(int j = 0; j < exceptionsc; j++)
{
if(exceptionsv[j] == i) i++;
}
printf("%d ", i);
}
}
for (char i=0; i<12; i++) { if (i%4 != 0) printf("%d", i); }
Or you can isolate the pattern and duplicate it using a nested loop :
char patternCount = 3;
for (char i=0; i<patternCount; i++) {
for (char j=1; j<4; j++) printf("%d", i*4 + j);
}

I couldn't handle to write a histogram

My aim is to generate a histogram for repeated numbers. The code works well until the frequency is bigger than 2.
I think I know what is wrong with the code (line 9) but I cannot find an algorithm to solve it. The problem that I have is when it writes the histogram, it separates and then gathers it again.
My Input:
5
5 6 6 6 7
Output:
6:2 6:2 6:3
but the output I need is
6:3
I kind of see the problem but I couldn't solve it.
#include <stdio.h>
int main(){
int array[25];
int i, j, num, count = 1;
scanf("%d", &num);
for (i = 0; i < num; i++) {
scanf("%d", &array[i]);
for (j = 0; j < i ; j++) {
if (array [i] == array[j]) {
count++;
printf("%d:%d ", array[i], count);
}
}
array [i] = array[j];
count = 1;
}
return 0;
}
You are trying to count occurrences before all units have been accepted, which is not possible unless you maintain a separate counter for each value, which in turn is not practical if there is no restriction on the input value range or the range is large.
You need to have obtained all values before you can report any counts. Then for each value in the array, test if the value has occurred earlier, and if not, iterate the whole array to count occurrences:
#include <stdio.h>
#include <stdbool.h>
int main()
{
// Get number of values
int num = 0 ;
scanf("%d", &num);
// Get all values
int array[25];
for( int i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
// For each value in array...
for( int i = 0; i < num ; i++)
{
// Check value not already counted
bool counted = false ;
for( int j = 0; !counted && j < i; j++ )
{
counted = array[j] == array[i] ;
}
// If current value has not previously been counted...
if( !counted )
{
// Count occurnaces
int count = 0 ;
for( int j = 0; j < num; j++ )
{
if( array[j] == array[i] )
{
count++ ;
}
}
// Report
printf("%d:%d ", array[i], count);
}
}
return 0;
}
For your example input, the result is:
5
5 6 6 6 7
5:1 6:3 7:1
It is possible to merge the two inner loops performing the counted and count evaluation:
// Count occurrences of current value,
bool counted = false ;
int count = 0 ;
for( int j = 0; !counted && j < num; j++ )
{
if( array[j] == array[i] )
{
count++;
// Discard count if value occurs earlier - already counted
counted = j < i ;
}
}
// If current value has not previously been counted...
if( !counted )
{
// Report
printf("%d:%d ", array[i], count);
}
}

How to remove duplicate elements from random array I generated?

So I have this code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char a[200], b = 0;
int x;
x = 100 + rand() % 200;
for (int i = 0; i < x; i++) {
a[i] = 'a' + rand() % 26;
}
for (int i = 0; i < x; i++) {
if (b % 10 == 0) {
printf("\n%c ", a[i]);
b = 0;
}
else {
printf("%c ", a[i]);
}
b++;
}
printf("\n");
return 0;
}
Purpose is that I should generate random array of letters from 'a' to 'z' (which I've managed to do) and after that print new array without elements that repeat in first array. I've tried implementing code from here for removing duplicate elements but it didn't worked in my code.
A simple solution is to loop over the array and copy each element to a new array, but first check that the value doesn't already exist in the new array.
An O(n) solution, assuming your array contains only letters a to z, consists of creating another small array of 26 integers initialized to zeroes, exist[26], then for each letter from the the main array,
if exist[letter - 'a'] > 0 don't print it
otherwise print it and increment exist[letter - 'a']
for instance,
int exist[26] = { 0 };
for(int i=0 ; i<x ; i++) {
if (exist[a[i] - 'a'] == 0) {
exist[a[i] - 'a']++;
printf("%c ", a[i]);
}
}
For starters the program has undefined behavior due to the statement
x = 100 + rand() % 200;
because the calculated value of the variable x can exceed the size of the array.
I think you mean
x = 1 + rand() % 200;
^^^
Also it is desirable to call the standard function srand to get different random sequences when the program runs.
The program can look the following way.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 200
int main(void)
{
char s[N];
size_t n;
srand( ( unsigned int )time( NULL ) );
n = 1 + rand() % N;
for ( size_t i = 0; i < n; i++ )
{
s[i] = 'a' + rand() % ( 'z' - 'a' + 1 );
}
for ( size_t i = 0; i < n; i++ )
{
putchar( s[i] );
if ( ( i + 1 ) % 10 == 0 || i + 1 == n ) putchar( '\n' );
}
putchar( '\n');
size_t m = 0;
for ( size_t i = 0; i < n; i++ )
{
size_t j = 0;
while ( j < m && s[j] != s[i] ) j++;
if ( j == m )
{
if ( m != i ) s[m] = s[i];
++m;
}
}
n = m;
for ( size_t i = 0; i < n; i++ )
{
putchar( s[i] );
if ( ( i + 1 ) % 10 == 0 || i + 1 == n ) putchar( '\n' );
}
putchar( '\n');
return 0;
}
Its output might be like
bimiwgnkew
tphzfidwmn
yqoyoxbbxd
kalxljfyvj
upzdoglrez
edsubgsfjr
kvrvscgadb
lxsmdhuoaz
bimwgnketp
hzfdyqoxal
jvursc

How to produce the following output?

Since i'm new in the programming world ,i'm facing little problem while writting program for this pattern .I tried many times but the result is not what i wanted ?
The pattern is :
1
23
456
78910
What i have written is :-
#include<stdio.h>
#include<conio.h>
void main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch() ;
}
#include <stdio.h>
int main()
{
printf("1\n23\n456\n78910\n");
return 0;
}
produces the output you desire
You need to print a newline after the inner loop:
#include<stdio.h>
#include<conio.h>
int main()
{
int num = 1 , j = 1 , x = 1 , i = 1 ;
while( j <= 4 ) {
while( i <= num ) {
printf( "%d", x ) ;
x++ ;
i++ ;
}
printf("\n");
num++ ;
i = ( i + 1 ) - num ;
j++ ;
}
getch();
return(0);
}
There is another example:
int main()
{
int i, j, num = 1, line = 4;
for(i = 1; i <= line ; i++)
{
for(j = 0; j < i; j++)
{
printf("%d", num);
num++;
}
printf("\n");
}
return 0;
}
With single loop:
# include<stdio.h>
# define LIMIT 100
int main(){
int i, prev=0, next=0, diff=1;
for(i=1;i<LIMIT;i++){
printf("%d", i);next++;
if(diff == next-prev){
printf("\n");
diff++;prev = next = 0;
}
}
}

Resources