Pattern Printing in C [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hey I am stuck to find a logic to print this pattern. I tried a lot but can't find any way.
My Problem is:
I have an array which contains some random integers and based on that I have to print stars(*) in vertically format.
For example:
array = [2,3,5,2,6];
Then the pattern will be:
*
* *
* *
** *
*****
*****

Here's how to do this in Java, without temporary arrays (Java Fiddle here):
public static void main(String []args){
final int[] array = new int[]{2,3,5,2,6};
int max = -1; // Maximum of all integers
for (int num : array) {
max = Math.max(max, num);
}
for (int vert = 0; vert < max; ++vert) {
for (int horz = 0; horz < array.length; ++horz) {
System.out.print(array[horz] >= max-vert ? '*' : ' ');
}
System.out.println();
}
}
Output:
*
* *
* *
** *
*****
*****

Here is what i suggest you do.
Initialize a 2D-array.
With rows equal to the Highest Number in Array. and Columns equal to Number of Integers in Array..
Then you can fill the array with corresponding number of stars, for specific index in array. hope that will at least make you understand One-of-Many logics.

int main(int argc, const char * argv[]) {
// insert code here...
int arr[4] = {2,1,4,3};
int max=4;
int count=0;
for(int i=0;i<4;i++)
{
while(count<4)
{
if(arr[count]>=max)
{
printf("*");
}
else
{
printf(" ");
}
count++;
}
count=0;
max--;
printf("\n");
}
return 0;
}

Related

It is a hackerrank problem, that deals with Dynamic arrays in C, So if anyone can explain me this program please [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
#include <stdlib.h>
/*
* This stores the total number of books in each shelf.
*/
int* total_number_of_books;
/*
* This stores the total number of pages in each book of each shelf.
* The rows represent the shelves and the columns represent the books.
*/
int** total_number_of_pages;
int main()
{
int total_number_of_shelves;
scanf("%d", &total_number_of_shelves);
int total_number_of_queries;
scanf("%d", &total_number_of_queries);
total_number_of_books = (int*)calloc(total_number_of_shelves, sizeof(int));
total_number_of_pages = (int**)calloc(total_number_of_shelves, sizeof(int *));
for (int i = 0; i < total_number_of_shelves; i++)
{
total_number_of_pages[i] = (int*)calloc(1100,sizeof(int));
}
while (total_number_of_queries--)
{
int type_of_query;
scanf("%d", &type_of_query);
if (type_of_query == 1)
{
int shelf, pages;
scanf("%d %d", &shelf, &pages);
total_number_of_books[shelf] += 1;
int i = 0;
while (total_number_of_pages[shelf][i] != 0)
{
i++;
}
total_number_of_pages[shelf][i] = pages;
} else if (type_of_query == 2) {
int shelf, number__ofpages;
scanf("%d %d", &shelf, &number__ofpages);
printf("%d\n", *(*(total_number_of_pages + shelf) + number__ofpages));
} else {
int shelf;
scanf("%d", &shelf);
printf("%d\n", *(total_number_of_books + shelf));
}
}
}
I feel like the solution is difficult to understand, and I have never faced problems of dynamic memory allocation like that before, I understood all the declarations, but most of if and else statements made me confused and I am did not understand what the program is trying to achieve.

How to correctly pass an array to struct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Question Edited
I'm very new in C, and here.
Is it possible for an array return its value via a struct?
Although I'm trying to process each character in List[5] = {1, 2, 3, 4}, however, it only sticks at 1 and only prints 575757 rather than
My struct
struct Count numbers() {
struct Count numbers;
int List[5] = {1, 2, 3, 4};
int i = 0;
for (i = 0; i < 10; i++) { //It might be something in my for loop
numbers.intOne= List[i] + 4; // 1 + 4
numbers.intTwo= List[i] + 6; // 1 + 6
return numbers;
}
};
This only prints 575757, i wish this to print 576879
void printCode(struct Count numbers) {
int i;
for (i = 0; i < 3; i++) {
printf("%i%i", numbers.intOne, numbers.intTwo);
}
}
The main
int main() {
int i = 0;
for (i = 0; i < 10; i++) {
numbers();
printCode(numbers());
getchar();
}
}
Thank you for any help!
it only stucks at '4352'
You are returning from decode soon after processing the first element of the output array which is 4352. So the other elements of check and param arrays do not get filled up.
In any case, check and param are arrays local to the decode function and the values of their elements cannot be used outside the function.

return -1 in C? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I was given code and I don't understand why the function returns -1. I feel that it is a typo. The code is given below:
int equilibrium(int array[], int size)
{
int sum = 0;
int left_sum = 0;
int i;
for (i = 0; i < size; i++) {
sum += array[i];
}
for (i = 0; i < size; i++) {
if (array[i] == sum - 2 * left_sum) {
return i;
}
left_sum += array[i];
}
return -1;
}
It looks like it uses -1 to mean "not found", it looks like a search function trying to find an index i where the condition in the innermost if is true.
equilibrium returns the equilibrium point of an array as an index in the array. When the array doesn't have an equilibrium point, the function returns -1 instead. It is used as followed:
int equilibrium_idx = equilibrium(somearrayvalue, somesize);
if( equilibrium_idx == -1 )
printf("It isn't balanced\n");
else
printf("The equilibrium index is %d\n", equilibrium_idx);

Sorting a list of integers in C before printing them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to print my final array elements in a sorted order.
I have made a group of three elements and I am finding the largest element of the group. I need to display the list in a sorted order.
This is the relevant pieces of my code:
#define MAX 9
void display(); //Display the element of array
int array[MAX]; //Array for Storing MAX element
//Function for displaying elements
void display()
{
int read_counter;
for(read_counter = 0; read_counter < MAX; read_counter++)
{
printf("\n Elements are %d\t",array[read_counter]);
}
}
How can I sort and print the elemnts of array ?
You can use qsort with a compare function:
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
And then use it to sort your array before printing:
qsort (array, 3, sizeof(int), compare);
If you are handling very large or very small numbers and are afraid of an overflow situation, use this compare method instead:
int compare(const void* a, const void* b)
{
int va = *(const int*) a;
int vb = *(const int*) b;
return (va > vb) - (va < vb);
}
You need to sort the array and then display it.
If you don't want the actual array to change you can keep the sorted array in an intermediate temporary array.
Do something like:
#define MAX 9
void display(); //Display the element of array
int array[MAX]; //Array for Storing MAX element
//Function for displaying elements
void display()
{
int min,i,j,tempArray[MAX],temp;
//copy into temporary array
for(i=0;i<MAX;i++)
tempArray[i] = array[i];
//sort temporary array (basic selection sort)
for(i=0;i<MAX-1;i++)
{
min = i;
for(j=i+1;j<MAX;j++)
{
if(tempArray[min] > tempArray[j] )
{
min = j;
}
//swap min with current if current is not already min
if(i != min)
{
temp = tempArray[i];
tempArray[i] = tempArray[min];
tempArray[min] = temp;
}
}
}
int read_counter;
for(read_counter = 0; read_counter < MAX; read_counter++)
{
printf("\n Elements are %d\t",tempArray[read_counter]);
}
}

Frequency Count of Number in an array in C is this code effective and efficient [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following code in c for counting frequency of number from array:
#define MAX 10
int flag=0;
void display(int no,int cnt,int visi[]);//function declaration
int main()
{
int arr[]={1,1,1,2,3,4,2,2,3,1};//asume any array or we can enter from user
int visited[MAX];
int i,j,no,cnt=1;
clrscr();
for(i=0;i<10;i++)//loop
{
no=arr[i];
cnt=1;
for(j=i+1;j<10;j++)
{
if(no==arr[j])
cnt++;
}
display(no,cnt,visited);
}
return 0;
}
void display(int no,int cnt,int visited[])
{
int static i;
int j;
if(flag==1)
for(j=0;j<=i;j++)
{
if(visited[j]==no)
goto a;
}
i++;
flag=1;
printf("\n%d=%d",no,cnt);
visited[i]=no;
a:
}
Please help to improve my code or suggest any other technology for effectiveness
is this algorithm effective and efficient or not please give feedback.
You can sort the array first by merge sort (O(n log n)) and then calculate the frequency of a number by single loop like this-:
int j=0;
for( i = 0; i < 9; i++ )
{
if (arr[i] == arr[i+1])
cnt++;
else
{
visited[j] = cnt;
cnt = 0;
j++;
}
}
to count frequency of numbers in array, try this code
#include <stdio.h>
#define MAX 10
int countArray[MAX];
int main()
{
int arr[]={1,1,1,2,3,4,2,2,3,1},i;
for(i=0;i<MAX;i++)
countArray[i]=0;
for(i=0;i<MAX;i++)
countArray[arr[i]]++;
for(i=0;i<MAX;i++)
{
if(countArray[i])
printf("%d %d\n",i,countArray[i]);
}
return 0;
}
You don't say this explicitly, but it looks as if you had an array of non-negative numbers whsoe values is smaller than MAX.
If the range of the numbers is known, you can create an array of counts. Visit each element of the array once and increment the count for that element. Then pass through the array of counts and output it as appropriate.
This method works well if the range of valid numbers and therefore the size of the count array is small. (You have the same problem for your visited array, whose size is the same as the size of an array of counts.)
The example below implements counting with an array of counts. The code also takes care of values that fall outside the valid range of MIN to MAX inclusively.
#include <stdio.h>
#define MIN 1
#define MAX 10
int main()
{
int arr[] = {1, 1, 1, 2, 3, 4, 2, 2, 3};
int narr = sizeof(arr) / sizeof(arr[0]);
int count[MAX + 1 - MIN] = {0};
int uncounted = 0;
int i;
for(i = 0; i < narr; i++) {
if (arr[i] < MIN || arr[i] > MAX) {
uncounted++;
} else {
count[arr[i] - MIN]++;
}
}
for(i = MIN; i < MAX + 1; i++) {
if (count[i - MIN]) {
printf("element %d ocurs %d times.\n", i, count[i - MIN]);
}
}
if (uncounted) {
printf("%d elements were not accounted for.\n", uncounted);
}
return 0;
}
int main()
Is implementation dependant. You do not wan't to use this, some compilers will not accept it.
Use
int main(void)
instead
for(i=0;i<10;i++)//loop
You already defined MAX, so why not use it there. Stuff like this makes code harder to maintain.
The efficiency is dependant on the input values you have. For small arrays with small numbers, this works fine otherwise.

Resources