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.
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 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 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 3 years ago.
Improve this question
I am facing issue copying data to void pointer array through for loop. I am not allowed to use double pointer.
I am trying someting like this:
for(i=0;i<9;i++){
array + i = i;
}
but I cant just do this, and I know that,
I am trying to do it like this
memcpy(
array+i,
i,
s
);
I have already allocated memory:
My actual code is some thing like this:
if(v->e_type==V_INT){
// printf("%p ",v->e_array+v->no_e);
memcpy(
v->e_array+v->no_e,
new_val,
v->e_sz
);
}
If you have something like this:
int *x = malloc(sizeof(int)*4);
You can put values in it with pointer arithmetic:
for(int i = 0; i < 4; i++) {
*(x+i) = i; // or *(x+1) = 0; for example
}
Or simply:
for(int i = 0; i < 4; i++) {
x[i] = i;
}
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 4 years ago.
Improve this question
Super noob question here, but I haven't worked much in C and I'm trying to create an array but it doesn't seem to be working. I have played around a bit in an online compilator but I just can't get it right.
What I want is an array containing 100 elements. I want the first element to be 8, the last element to be 12, and every element should increase by 0.04. So [8, 8.04, 8.08, ..... , 11.96, 12].
Can anyone help a newbie out? :)
#define NUMS 101
int main()
{
double arr[NUMS];
double start = 8.0, end = 12.0;
double gap = (end - start) / (NUMS - 1);
int i;
for (i = 0; i < NUMS; ++i)
arr[i] = start + i * gap;
}
Here is code example based on Blaze snipped. This code first fills array and then prints it out. Also as jwismar mentioned you need 101 elements.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
float a[101];
for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++)
a[i] = 8.0 + (i*0.04);
for (int i = 0; i < sizeof(a)/sizeof(a[0]); i++) {
printf("%f\n",a[i]);
}
return 0;
}
BTW if you want to start from 8 and end with 12 then you need 101 elements.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double arr[101];
int i;
arr[0] = 8;
for (i = 1; i < 101; i++)
arr[i] = arr[i - 1] + 0.04;
for (i = 0; i < 101; i++)
printf("%f\n",arr[i]);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to create a program who store the rest of successive division in the array.But unfortunately the array i created refuse store the remain correctly for example 5/2 the remain is suppose to be 1 but the array store another value
#include <stdio.h>
#include <stdlib.h>
int divise(int n){
int i=0;
int remain[20];
int rest;
while(n!=0){
rest = n%2;
remain[i] = rest;
n = n/2;
i++;
printf("%d\n",remain[i]);
}
}
int main(){
divise(10);
}
The mistake is with your i++ statement . It should be after printf("%d\n",remain[i]);.
Modified code :-
#include <stdio.h>
#include <stdlib.h>
int divise(int n)
{
int i = 0;
int remain[20];
int rest;
while (n != 0)
{
rest = n % 2;
remain[i] = rest;
n = n / 2;
printf("%d\n", remain[i]);
i++; // repositioned
}
}
int main()
{
divise(10);
return 0;
}
Output :-
0
1
0
1
Your function int divise(int n) do not return any int values . So better make it void divise(int n) .Also int main() should have a return 0 .
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this C program code here, which determines the highest contiguous value for an array:
#include <stdio.h>
int inputs[8];
int calcSum(int i, int j);
int main(void) {
int i, j, maxSum = 0, tempSum = 0;
int length = sizeof(inputs)/sizeof(inputs[0]);
for(i=0;i<length;i++) {
scanf("%d", &inputs[i]);
}
for(i=0;i<length;i++) {
for(j=i;j<length;j++) {
tempSum = calcSum(i,j);
if(tempSum > maxSum) {
maxSum = tempSum;
}
}
}
printf("%d\n", maxSum);
return 0;
}
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Even though this code looks correct to me, it outputs a wrong result. Sampling adding any sort of printf("") (can be empty as well) between tempSum = ... and if(tempSum >...) will make the code output the correct answer for all test cases. I even rewrote the entire code from scratch and still get the same issue.
For example, the number series: 5 2 -1 -2 -4 3 5 -6 should output 8, which it does once the printf("") is added, otherwise it outputs 38...and I have no idea why. Can you please explain, where I went wrong?
In this code:
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
You need to make sure you initialize sum to a starting value:
int calcSum(int i, int j) {
int c, sum;
sum=0;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}