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
Related
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);
}
}
I am having a problem arranging 25 numbers into 5 by 5 order. This is what I have so far:
int main() {
int num,arr[100];
int i, y, swap;
num = 25;
printf("Enter %d integers \n",num);
for(i = 0; i < 25; i++) {
printf("\nElement %d: ", i + 1);
scanf("%d", &arr[i]);
}
for(i = 0; i < ( num - 1 ); i++) {
for(y = 0 ; y < num - i - 1; y++) {
if(arr[y] > arr[y+1]) {
swap = arr[y];
arr[y] = arr[y+1];
arr[y+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < num ; i++ )
printf("%d \t", arr[i]);
getch();
}
I am expecting my output will be like this.
Sorted list in ascending order:
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
Like this:
...
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < 5; i++ ) {
printf("%c%d", (i % 5) ? '\t' : '\n', arr[i]);
}
printf("\n");
...
Or, after "chux", without the leading newline:
...
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < num ; i++ ) {
printf("%d%c", arr[i], ((i % 5) != (5-1)) ? '\t' : '\n');
}
...
And, if you want the columns nicely aligned, add an appropriate width to the %d:
printf("%10d ... "); /* right aligned */
or
printf("%-10d ... "); /* left aligned */
Just one modification:
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < num ; i++ )
{
printf("%d \t", arr[i]);
if(i % 5 == 0) putchar('\n');
}
Avoid printing extra space-tab after the last number.
To get visually lined up values, use a print width.
int print_width = 4;
int columns = 5;
for (i = 0 ; i < num ; i++ ) {
printf("%*d", print_width, arr[i]);
putchar(i%columns == columns-1 ? '\n' : '\t');
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
void insertionSort(int ar_size, int* ar) {
int i, j;
int temp = 0;
for (i = 1; i < ar_size; i++) {
j = i;
while (j > 0 && ar[i - 1] > ar[i]) {
temp = ar[i - 1];
ar[i - 1] = ar[i];
ar[i] = temp;
j--;
}
}
for (j = 0; j < ar_size; j++) {
printf("%d", ar[j]);
printf(" ");
}
}
int main(void) {
int _ar_size;
scanf("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for (_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
scanf("%d", &_ar[_ar_i]);
}
insertionSort(_ar_size, _ar);
return 0;
}
I have been trying to look for the error. I cannot see any. What is wrong with this code?
For input as 6 and 4 1 3 5 6 2 , it gives output as 1 3 4 5 2 6. There is one less iteration of the loop but I cannot see why? Please help. Thanks.
You are using index i instead of index j in the internal loop of the function.
while(j>0 && ar[i-1]>ar[i])
{
temp = ar[i-1];
ar[i-1] = ar[i];
ar[i] = temp;
j--;
}
Here everywhere index j has to be used.
Also it is a bad idea that the function also outputs the sorted array. It should do only sorting.
The other bad idea is to use identifiers that start with undescores.
It is better when the first parameter is an array and the second parameter is the size of the array.
The code could look the following way
#include <stdio.h>
void InsertionSort( int *a, int n )
{
int i;
for ( i = 1; i < n; i++ )
{
int j = i;
while ( j > 0 && a[j-1] > a[j] )
{
int tmp = a[j-1];
a[j-1] = a[j];
a[j] = tmp;
--j;
}
}
}
int main(void)
{
int size;
scanf( "%d", &size );
int a[size];
int i;
for ( i = 0; i < size; i++ ) scanf( "%d", &a[i] );
for ( i = 0; i < size; i++ )
{
printf( "%d ", a[i] );
}
puts( "" );
InsertionSort( a, size );
for ( i = 0; i < size; i++ )
{
printf( "%d ", a[i] );
}
puts( "" );
return 0;
}
If the input is
10
2 7 5 4 9 1 4 8 3 5
then output is
2 7 5 4 9 1 4 8 3 5
1 2 3 4 4 5 5 7 8 9
You seem to be using the wrong iterator when pushing the value to the front.
while(j>0 && ar[j-1]>ar[j]) {
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
j--;
}
Try performing a dry run on a piece of paper, it's easy to spot the problem.
for (i = 1; i < ar_size; i++) {
j = i;
while (j > 0 && ar[i-1] > ar[i]) { // problem begins here
temp = ar[i-1];
ar[i-1] = ar[i];
ar[i] = temp;
j--;
}
}
i does not change in the inner loop.
At some point your program swaps 6 with 2 and your sample input list becomes 1 3 4 5 2 6, a[i-1] is 2 and a[i] is 6 and because of ar[i-1] > ar[i] condition the program's flow does not go into the inner loop.
Try this fix:
for (i = 1; i < ar_size; i++) {
j = i;
while (j > 0 && ar[j-1] > ar[j]) {
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
j--;
}
}
The problem with your insertion-sort is you have your j indexes replaced with i indexes in the following code:
while(j>0 && ar[j-1]>ar[j])
{
temp = ar[j];
ar[j] = ar[j-1];
ar[j-1] = temp;
j--;
}
Just a note, when requesting input, it is good practice to print a brief statement describing the input expected. (yes, quick a dirty testing is an exception) It makes if much easier to avoid mistakes with something like:
printf ("enter array size: ");
scanf ("%d", &_ar_size);
int _ar[_ar_size], _ar_i;
for (_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
printf ("enter array element[%d] : ", _ar_i);
scanf ("%d", &_ar[_ar_i]);
}
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;
}
}
}
I'm trying to write a code that could print something like this
-xv
-xvv
-xvvv
-xvvvv
-xvvvvv
-xxv
-xxvv
-xxvvv
-xxvvvv
-xxvvvvv
-xxxv
-xxxvv
-xxxvvv
-xxxvvvv
-xxxvvvvv
The extra spacing between the 3 "groups" are just for clarity's sake. The maximum number of '-' is 1, 'x' is 3, 'v' is 5, and the number of each symbol increments.
To draw this diagram, I have the following code
for (k = 1 ; k <= num_dash ; k++)
{
for (i = 1 ; i <= num_x ; i++)
{
for (j = 1 ; j <= num_v ; j+++)
{
for (k1 = 0 ; k1 < k ; k++)
printf("-");
for (i1 = 0 ; i1 < i ; i++)
printf("x");
for (j1 = 0 ; j1 < j ; j++)
printf("v");
printf("\n");
}
}
}
This is when I know there are 3 different kinds of symbols. Is it possible to do the same if the number of symbols are only known at runtime? For example, what if I want the same program to also be able to print
xv
xvv
xvvv
xvvvv
xvvvvv
xxv
xxvv
xxvvv
xxvvvv
xxvvvvv
xxxv
xxxvv
xxxvvv
xxxvvvv
xxxvvvvv
In this case, my code would only be
for (i = 1 ; i <= num_x ; i++)
{
for (j = 1 ; j <= num_v ; j+++)
{
for (i1 = 0 ; i1 < i ; i++)
printf("x");
for (j1 = 0 ; j1 < j ; j++)
printf("v");
printf("\n");
}
}
And there are only 2 for-loops instead of 3. Can I write my code such that the number of for-loops varies?
Use functions instead of innermost for loops and pass the characters and number of iterations as parameters to that function.
E.g.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_aux(char **symbols, int *times, int len, int pos, char *acc){
int i;
char *p;
if(pos == len){
printf("%s\n", acc);
return;
}
p = malloc(sizeof(char)*strlen(symbols[pos])*times[pos]+strlen(acc)+1);
for(i=0;i<times[pos];++i){
int j;
*p='\0';
strcpy(p, acc);
for(j=0;j<=i;++j){
strcat(p, symbols[pos]);
}
print_aux(symbols, times, len, pos + 1, p);
}
if(pos + 1 == len)// when last symbol
printf("\n",pos);
free(p);
}
//wrap function
void print(char **symbols, int *times, int len){
print_aux(symbols, times, len, 0, "");
}
int main() {
int i,n;
char **symbols;
int *times;
fprintf(stderr,"number of symbols :");
scanf("%d", &n);
symbols=(char**)malloc(sizeof(char*)*n);
times = (int*)malloc(sizeof(int)*n);
for(i=0;i<n;++i){
char wk[128];
fprintf(stderr,"input symbol [%d]:", i+1);
scanf(" %s", wk);
symbols[i] = strdup(wk);
fprintf(stderr,"maximum number of \"%s\":",wk);
scanf(" %d", ×[i]);
}
print(symbols, times, n);
{ //release the allocated area
for(i=0;i<n;++i){
free(symbols[i]);
}
free(symbols);
free(times);
}
return 0;
}