Printing stars for a specific number of freq in arrays - c

I am still new to C, and I need to figure a way to print the same number of stars as the number of frequencies.
Here's my code:
int arrayHistogram(int array[]){
int i;
int j;
int count=0;
int freq[SIZE];
char stars[SIZE];
for(i = 0; i < SIZE; i++){
count =1;
for(j = i+1; j < SIZE; j++){
if(array[i]==array[j]){
count++;
freq[j]=0;
}
}
if(freq[i] != 0){
freq[i] = count;
}
}
//for ( i = 0; i < SIZE; i++){
//int num = freq[i];
//stars[i]= '*'*num;
}
printf("Value ");
printf("Frequency ");
printf("Histogram\n");
for(i = 0; i < SIZE; i++){
if(freq[i] != 0){
printf("%5d%10d%10d\n", array[i], freq[i], stars[i]);
}
}
}
I know that I can't multiply char by int here to print the stars, but I put here just to show where I need to print those stars (i.e. the histogram).
My Output:
Value Frequency Histogram
7 4 -46
8 3 126
4 6 84
Expected output:
Value Frequency Histogram
7 4 ****
8 3 ***
4 6 ******

Well, you already know everything to do that i.e. loop.
Simply, loop up to a frequency and print that many stars.
Something like this (pseudo code):
loop until freq[i]
print '*'
end loop
It's just an idea. You know how a loop works. Just put a nested loop (i.e. for loop) where you need to print the stars. Mind the newline. You need a newline at the end of each line after printing the stars.
UPDATE:
As observed, you've frequencies up to 10 i.e. a fixed size that you already know. You can simply use a string of stars and then print it using printf() with %.*s format specifier with field width and string length.
Here's an example (live code):
#include <stdio.h>
#define SIZE 10
int main(void)
{
const char* stars = "**********";
const int freqs[ SIZE ] = { 3, 5, 6, 2, 0, 7, 10, 9, 4, 8 };
for ( int i = 0; i < SIZE; i++ )
{
printf("%5d \t %10.*s\n", freqs[i], freqs[i], stars);
}
return 0;
}
OUTPUT:
3 ***
5 *****
6 ******
2 **
0
7 *******
10 **********
9 *********
4 ****
8 ********

how about building char stars[SIZE] with a loop?
int nStars = freq; // in you situation.
for(int i=0; i < nStars;++i)
{
stars[i] = '*'; // set char i to be '*'
}
stars[nStars] = '\0'; // don't forget to terminate your string
you can make a simple utility function void fillChar(char* buff, char ch, int count); of it.
Take Caution not to exceed the size of the allocated string or bad things gona happen.
Cheers.

Related

Is there any way to create loops based on user input?

I wanna create all possible 5 digit numbers that can be created from the numbers (0-7).
The code below achieves this, but is there any way to make this depend on user input?
The number of loops equals the number of digits I want and each individual loop must be:
for(1st number;condition<=last number;1st number++)
So, for five digits, I have:
for(i=0;i<8;i++){
for(j=0;j<8;j++){
for(k=0;k<8;k++){
for(m=0;m<8;m++){
for(n=0;n<8;n++){
printf("%d %d %d %d %d\n",i,j,k,m,n);
}
}
}
}
}
Keep iterators in an array and increment them manually.
#include <assert.h>
#include <stdio.h>
#include <string.h>
void callback(unsigned n, int i[n]) {
assert(n == 5);
printf("%d %d %d %d %d\n", i[0], i[1], i[2], i[3], i[4]);
}
void iterate(unsigned n, unsigned max, void (*callback)(unsigned n, int i[n])) {
// VLA, use *alloc in real code
int i[n];
memset(i, 0, sizeof(i));
while (1) {
for (int j = 0; j < n; ++j) {
// increment first number, from the back
++i[n - j - 1];
// if it didn't reach max, we end incrementing
if (i[n - j - 1] < max) {
break;
}
// if i[0] reached max, return
if (j == n - 1) {
return;
}
// if the number reaches max, it has to be zeroed
i[n - j - 1] = 0;
}
// call the callback
callback(n, i);
}
}
int main() {
// iterate with 5 numbers to max 8
iterate(5, 8, callback);
}
The beginning and ending of what the code prints:
0 0 0 0 0
0 0 0 0 1
...
...
7 7 7 7 6
7 7 7 7 7
If you want variable numbers of loops, you generally need to use recursion.
Say if you want n digits, with the ith digit be in the range of a[i],b[i], then you will do the following:
/* whatever */
int n;
int *a,*b,*number;
void recursion(int whichdigit){
if (whichdigit==n){
/* Say you managed to output number */
return;
}
for (int i=a[whichdigit];i<=b[whichdigit];i++){
number[whichdigit]=i;
recursion(whichdigit+1);
}
return;
}
int main(){
/* Say somehow you managed to obtain n */
a=malloc(n*sizeof(int));
b=malloc(n*sizeof(int));
number=malloc(n*sizeof(int))
if (!a||!b||!number){
/* unable to allocate memory */
}
/* Say somehow you managed to read a[i],b[i] for all i in 0..n-1 */
recursion(0);
return 0;
}
Warning: if you tries to have too many digits, you will likely get a segmentation fault or stack overflow error.

reverse an array with recursion

When I try to run this it asks me to input 11 numbers instead of 10 with is really weird then it out puts like an even weirder result please help.
void function(int array[],int length,int start)
{
if (length<start)
{
return;
}
int temp=array[start];
array[start]=array[length];
array[length]=temp;
function(array,length-1,start+1);
}
int main()
{
int array[10],num=0,num2=10;
printf("enter the array:\n");
for (int i =0; i<num2; i++)
{
scanf("%d\n",&array[i]);
}
function(array,num2,num);
for (int t = 0; t<num2; t++)
{
printf("%d\n",array[t]);
}
}
then this is the out put for the array 1,2 ... 10,11
enter the array:
1
2
3
4
5
6
7
8
9
10
11
return
214696143
10
9
8
7
6
5
4
3
2
(lldb)
please help
The last index of an array with size 10 is actually 9. So this line:
function(array,num2,num);
Should be:
function(array,num2-1,num);
Also, your use of scanf is incorrect. Eliminate the \n character. Read more about scanf here: http://www.cplusplus.com/reference/cstdio/scanf/
I corrected your code and tested it here: https://onlinegdb.com/B1Yy4yBqN
Try this:
#include <stdio.h>
void function(int array[],int start, int end)
{
if (start < end)
{
int temp;
temp = array[start];
array[start] = array[end];
array[end] = temp;
function(array, start+1, end-1);
}
}
int main()
{
int array[10],num=0,num2=10;
printf("enter the array:\n");
for (int i =0; i<num2; i++)
{
scanf("%d",&array[i]);
}
function(array,num,num2 - 1);
for (int t = 0; t<num2; t++)
{
printf("%d\n",array[t]);
}
}
Note that in C, the array starts from 0. The last index is 9 not 10. You need to access to the last element using length - 1 instead of length

Printing a Diamond of Numbers in C

int print_pattern(){
int x;
int y;
int i;
//for loop for the bottom and the top, 0 is the top and 1 is the bottom while it stops at anything above 2.
for (i = 0; i<2;i++){
//loop to the current number
for (x=1;x<=input;x+=2){
// top or bottom, this is the top because i had made the top of the diamond the 0
// therefore this makes my diamond print the top of the function.
if ( i == 0){
//starts for the top of the diamond. and counts the spaces.
for (y=1; y<=input-x; y++){
printf(" ");
}
//starts the printing of the diamond.
for (y=1; y<2*x;y++){
printf("%d ", y);
}
}
//bottom of the diamond, which is from the 1. For this spot it take in the one from the for loop to
// this if statement.
if (i==1){
//counting spaces again
for(y = 1; y<=x; y++){
printf(" ");
}
//printing again but this is the bottom of the pyramid. #really need to comment more
for(y = 1; y<(input-x)*2;y++){
printf("%d ", y);
}
}
//next line starts when printing out the numbers in the output.
printf("\n");
}
}
}
The output is supposed to look like a diamond of the numbers ending with the odd numberat each row. but it is going +2 number past the input and then also not printing the last line. Which should have a single one.
1 1
1 2 3 1 2 3 4 5
1 2 3 4 5 1 2 3 4 5 6 7 8 9
1 2 3 1 2 3 4 5 6 7
1 1 2 3
The left is what is expected and the right is what I currently am getting when inputting 5.
Because you already increment x by 2 in the upper part, you don't need to let the print loop run to y<2*x. It should probably just run to x.
The print loop in the lower part suffers from the fact that y<(input-x)*2 should probably be y<input-x*2 (you want to print 2 less each time).
Generally I'd try to name variables in a more speaking way, like printStartPosition, maxNumToPrint, stuff like that. That makes it easier by a surprising margin to understand a program.
As an enhancement, the two code blocks depending on the i value inside the x loop are structurally very similar. One could try to exploit that and collapse both of them into a function which gets a boolean parameter like "ascending", which increments y when true and decrements it when false. Whether that improves or hinders readability would have to be seen.
Also, keep your variables local if possible.
Peter Schneider has already raised some valid points in his answer.
Think about what you have to do when you print a diamond of height 5:
print 1 centered;
print 1 2 3 centered;
print 1 2 3 4 5 centered;
print 1 2 3 centered;
print 1 centered.
Sou you could write a function that prints the numbers from 1 to n centered in a line and call it with n = 1, 3, 5, 3, 1. This can be achieved with two independent loops, one incrementing n by 2, the other decrementing it.
Another approach is to recurse: print the lines as you go deeper, incrementing n by 2 until you reach the target width, at which point you don't recurse, but return and print lines with the same parameters again as you go up. This will print each line twice except the middle one.
Here's a recursive solution:
#include <stdlib.h>
#include <stdio.h>
void print_line(int i, int n)
{
int j;
for (j = i; j < n; j++) putchar(' ');
for (j = 0; j < i; j++) printf("%d ", (j + 1) % 10);
putchar('\n');
}
void print_pattern_r(int i, int n)
{
print_line(i, n); // print top as you go deeper
if (i < n) {
print_pattern_r(i + 2, n); // go deeper
print_line(i, n); // print bottom as you return
}
}
void print_pattern(int n)
{
if (n % 2 == 0) n++; // enforce odd number
print_pattern_r(1, n); // call recursive corefunction
}
int main(int argc, char **argv)
{
int n = 0;
if (argc > 1) n = atoi(argv[1]); // read height from args, if any
if (n <= 0) n = 5; // default: 5
print_pattern(n);
return 0;
}
A JAVA STAR PATTERN PROGRAM FOR DIAMOND SHAPE converted to C Program. Code comment will explain the changes and flow.
#include <stdio.h>
#include <string.h>
void myprintf(const char* a) {
static int iCount = 0;
if (strcmp(a, "\n") == 0) {
iCount = 0; //if it is new line than reset the iCount
printf("\n"); //And print new line
} else
printf(" %d", ++iCount); //Print the value
}
void main() {
int i, j, m;
int num = 5; //Enter odd number
for (i = 1; i <= num; i += 2) { //+=2 to skip even row generation
for (j = num; j >= i; j--)
printf(" ");
for (m = 1; m <= i; m++)
myprintf(" *"); //display of star converted to number
myprintf("\n");
}
num -= 2; //Skip to generate the middle row twice
for (i = 1; i <= num; i += 2) { //+=2 to skip even row generation
printf(" ");
for (j = 1; j <= i; j++)
printf(" ");
for (m = num; m >= i; m--)
myprintf(" *"); //display of star converted to number
myprintf("\n");
}
}
Output:
1
1 2 3
1 2 3 4 5
1 2 3
1
Here's the short code for such a diamond.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int w = 9;
int l;
for(l=0; l < w; ++l)
{
printf("%*.*s\n", abs(w/2 - l)+abs((2*l+1)-(2*l+1>w)*2*w), abs((2*l+1)-(2*l+1>w)*2*w), "123456789");
}
return 0;
}

How to print from 1 to 9 with three numbers in a line using loops

I am coding a tic tac toe game in C. I am stuck at making a board like this:
1 2 3
4 5 6
7 8 9
I want to use loops so that I dont have to use a printf function with many \n's and \t's...
Here's my attempt:
for (i=0;i<=9;i++)
{
printf("\n\n\n\t\t\t");
for (j=i;j<=i+2;j++)
{
printf("%c\t",boarddots[j]);
}
if (i==3)
break;
}
Something like this, you could adapt it to your actual needs:
for(int i = 1; i <= 9; ++i)
{
printf("%d", i); // print numbers one by one
if (0 == i % 3)
printf("\n"); // start new line if current number is divisible by 3
}
P.S. Sorry for possible typos
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
printf("%d ", (row * 3) + column + 1);
}
printf ("\n");
}
/*
output:
1 2 3
4 5 6
7 8 9
*/
Your loop condition for (i=0;i<=9;i++) iterated once too many. Personally, I would uses a 2D array such as char board [3][3], but one step at a time to help with your immediate question.
#include<stdio.h>
char boarddots[] = "--O-XX-O-";
int main()
{
int i;
for (i=0; i<9; i++) {
if (i % 3 == 0)
printf("\n\n\n\t\t");
printf("\t%c",boarddots[i]);
}
return 0;
}
Could do print as a string and use string truncation:
char boarddots[9] = {'1','2','3','4','5','6','7','8','9'};
int loop;
for (loop=0; loop<9; loop+=3)
printf ("%.3s\n", &boarddots[loop]);
You don't need a NULL on the end of the char array as the truncation takes care of that.

Reading an array of integers and printing them out

I'm learning C on my own and doing a few exercises. The following code reads in an
array of integers from the user. The integers are printed out when the user types in a "0" or when the array is filled. Now the problem is the output. When I type in "0" after I have typed in 3 digits e.g. 1 2 3 the output is the following: 1 2 3 -858993460 -858993460. I am not sure why I get the value "-858993460" but I have already found a solution to avoid it. Now my question is what the values mean and if there is a smarter solution than mine which is presented below as comments.
#include <stdio.h>
#include <string.h>
#define arraylength 5
int main ()
{
//const int arraylength = 21; //alternative possibility to declare a constant
int input [arraylength] ;
int temp = 0;
//int imax = 0;
printf("Please type in a your digits: ");
for (int i = 0; i < arraylength; i++)
{
scanf("%d", &temp);
if ( temp !=0)
{
input[i]= temp;
//imax= i;
}
else
{
//imax= i;
break;
}
if (i < arraylength-1)
printf("Next: ");
}
for (int i =0; i < arraylength; i++ ) // switch arraylength with imax
{
printf("%d", input[i]);
}
getchar();
getchar();
getchar();
}
This happens because irrespective of when the 0 input is given you print all 5 numbers:
for (int i =0; i < arraylength; i++ )
To fix this you can print only the number(s) user entered before entering a 0 by running a loop from 0 to i:
for (int j =0; j < i; j++ )
Those 2 numbers are the garbage that was left in the memory locations for the last 2 parts of your array. You never initialise those when you only input 3 numbers, so when you go through and print all 5 elements in the array, it prints whatever garbage was in the memory.
You print all integers in array which is size of arraylength = 5. So you get 5 integers in output. As you didn't initialize array, you get uninitilized values as 4th and 5th elements of array. You can use memset(&input, 0, arraylength*sizeof(int)); to set initials values in array to 0.

Resources