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 6 years ago.
Improve this question
I want to print the shape of numbers using 2D arrays. What I am saying is just like this. Think I want print num 2.
111111
11
111111
11
111111
I already tried, starting like this, but I cant build it further.
#include <stdio.h>
int main(){
int Num[5][6]= {{111111},
{000011},
{111111},
{110000},
{111111}};
int i,j;
for(i=0;i<6;i++){
for(j=0;j<7;j++){
printf("%d",Num[i][j]);
}
}
return 0;
}
Instead of spaces i have included "0" in my code.
You probably want this:
#include <stdio.h>
int main() {
int Num[5][6] =
{
{ 1,1,1,1,1,1 },
{ 0,0,0,0,1,1 },
{ 1,1,1,1,1,1 },
{ 1,1,1,1,0,0 },
{ 1,1,1,1,1,1 }
};
int i, j;
for (i = 0; i<5; i++) {
for (j = 0; j<6; j++) {
if (Num[i][j] == 1)
printf("1");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Disclaimer: there is still room for improvement. The proposed solution is as close as possible to the original code.
Related
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.
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 1 year ago.
Improve this question
I want to make a function that multiplies each element in two arrays and store it in the new array.
For example: amount[i] = price[i] * quantity[i];
My code doesn't work. Here is my code:
#include <stdio.h>
void extend(float[], float[], double[]);
int main(void) {
float price[10] = { 10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98 };
float quantity[10] = { 4, 8.5, 6, 8.35, 9, 15.3, 3, 5.4, 2.9, 4.8 };
double amount[10] = { 0 };
extend(price, quantity, amount);
for (int i = 0; i < 10; i++) {
printf("%f ", amount[i]);
}
return 0;
}
void extend(float PRICE[], float QUANTITY[], double amount[]) {
for (int i = 0; i < 10; i++) {
amount[i] = PRICE[i] * QUANTITY[i];
}
}
Please let me know which part is wrong.
code is work on codeblocks. No any error.
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 2 years ago.
Improve this question
Can you guys please help me with this code. I know this is basic but I am now really obsessed with it. I am trying to implement a very simple version of mergesort, but the output is all wrong. Please try to fix this code instead of writing a completely new one.
#include <stdio.h>
void merge_them(int a[], int l, int m, int r)
{
int i=l, j=m+1;
int final[r-l+1];
int p=0;
for(int k=0; k<=r-l+1; k++)
{
if(a[i]<a[j])
{
final[p]=a[i];
i++;
p++;
}
else if(a[i]>=a[j])
{
final[p]=a[j];
j++;
p++;
}
}
j=0;
for( i=l; i<r+1; i++)
{
a[i]=final[j];
j++;
}
}
void merge(int a[], int l, int r)
{
if(l<r)
{
int m = (l+r)/2;
merge(a,l,m);
merge(a,m+1,r);
merge_them(a,l,m,r);
}
}
int main()
{
int a[10] = {10,9,8,7,6,5,4,3,2,1};
merge(a,0,9);
for(int i=0; i<10; i++)
printf("%d ",a[i]);
}
UPDATE: This was not a homework. I was just revisiting some sorting algos based on recursion and tried to implement Merge sort. Anyway, I switched to while loop and used a more precise condition check, which led to correct sorted outputs.
There are actually three bugs:
The condition k<=r-l+1 in the for statement is wrong and it will have it do one more loop. It should be k<r-l+1 (< should be used instead of <=) like the condition i<r+1 used later.
The condition a[i]<a[j] is wrong because it isn't checking if the parts are used up. It should be j > r || (i <= m && a[i]<a[j]).
if(a[i]>=a[j]) should be removed because if you don't take an element from one part, you will obviously take from another part.
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 4 years ago.
Improve this question
so I got a task at my Uni a week ago:
There is n*n matrix given which is a square matrix, but I have to find max of these elements on the picture, question is how to do it ?
Well, I won't just do your homework but here is some code that you can consider a hint to get you started.
#include <stdio.h>
void printRelevant(int n)
{
for(int r=0; r<n; ++r)
{
for(int c=0; c<n; ++c)
{
if (r < n/2 || c > r || c < n-r-1)
{
printf("-");
}
else
{
printf("X");
}
}
printf("\n");
}
}
int main(void) {
printRelevant(17);
return 0;
}
Output:
-----------------
-----------------
-----------------
-----------------
-----------------
-----------------
-----------------
-----------------
--------X--------
-------XXX-------
------XXXXX------
-----XXXXXXX-----
----XXXXXXXXX----
---XXXXXXXXXXX---
--XXXXXXXXXXXXX--
-XXXXXXXXXXXXXXX-
XXXXXXXXXXXXXXXXX
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 5 years ago.
Improve this question
I am new to programming and have just started with array. This was my code for the problem
#include <stdio.h>
int main(){
int i=1,f=1,num,fac[10],sum=0;
for (num=1; num<=10; num++) {
for (i; i<=num; i++) {
f = f * i;
}
fac[num-1]=f;
}
for(i;i<=9;i++)
sum = sum + fac[i] * fac[i+1];
printf("The sum is %d",sum );
return 0;
}
Output it is giving is-The sum is 0
So what are the corrections to be made or any other code for the problem?
You need to reinitialize the variable i more often.
Instead of having a for loop that looks like for (i; i<=N; i++), initialize i and do for (i=0; i<=N; i++)