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.
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 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.
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.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I'm calling a simple method from a header file to the main C file and it's not working. Any clue?
Main
#include "header.h"
int main() {
int a = 2;
int b = 5;
int numArray[2] = {a, b};
displayNumbers(numArray, 2);
doubleIt(a);
doubleIt(b);
displayNumbers(numArray, 2);
return(0);
}
Header
int doubleIt(int x) {
return 2 * x;
}
void displayNumbers(int x[], int numSize) {
int i;
for (i = 0; i < numSize - 1; i++) {
printf("%d, ", x[i]));
printf("%d", x[numSize - 1]);
printf("\n");
}
The doubleIt method doesn't work.
If i understand your question properly, you want to double the array and print the values, in your case if a = 2 and b = 5 then you want to double to a = 4 and b = 10.
Modify these lines in your code as follows;
numArray[0] = doubleIt(a);
numArray[1] = doubleIt(b);
Hope this helps.
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;
}