The 2 arrays wont store input, the increment of each index number is always at 0 as printf states it.
int main()
{
int ingredientsAmount;
double ingredientsPrice[10];
double ingredientsWeight[10];
scanf("%d", &ingredientsAmount);
for(int i = 0; i < ingredientsAmount; i++)
{
scanf("%lf", &ingredientsPrice[i]);
printf("Price stored at index %lf\n", i);
scanf("%lf", &ingredientsWeight[i]);
printf("Weight stored at index %lf\n", i);
}
return 0;
}
You are using the incorrect conversion specifier %lf with an object of the type int. Use the conversion specifier %d. Write
printf("Price stored at index %d\n", i);
and
printf("Weight stored at index %d\n", i);
Or maybe you mean the following
printf("Price stored at index %d is %f\n", i, ingredientsPrice[i]);
and
printf("Weight stored at index %d is %f\n", i, ingredientsWeight[i]);
Pay attention to that the for loop is unsafe because the value of the variable ingredientsAmount entered by the user can be greater than the sizes of the declared arrays.
At least you should write
if ( scanf("%d", &ingredientsAmount) != 1 || ingredientsAmount > 10 )
{
ingredientsAmount = 10;
}
Related
Why didn't this code work after adding "res=pow(arr[i],x)"
I want it to print like this
"printf("%d * %d = %d \n",i+1,x,pow(arr[i],x));"
The code doesn't work untill i print like this
"printf("%d * %d = %d \n",i+1,x,res)));"
#include<stdio.h>
#include<math.h>
int main()
{
int arr[5];
for(int i=0 ; i<5 ; i++){
printf("enter the numbers %d\n",i+1);
scanf("%d",&arr[i]);
}
int x;
printf("what is power would you like...\n");
scanf(" %d",&x);
printf("The power of the array elements is...\n");
for(int i=0 ; i<5 ; i++){
printf("%d * %d = %d \n",i+1,x,pow(arr[i],x));
}
return 0; // 1*2=1*1 , 3*2=3*3
}
pow returns a double. You need %lf format for the third argument or you'll get undefined behaviour trying to format a floating point value with an integer %d format (most compilers issue a warning about this BTW).
quickfix
printf("%d * %d = %lf \n",i+1,x,pow(arr[i],x));
Assigning the result to an integer workarounds the issue. That's why it works then (but sometimes it leads to rounding errors so beware!)
You may have a look at integer power algorithms instead. pow is more suited for floating point operations.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int mult;
int n;
int ans;
ans = mult * i;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 0; i<n; i++) {
printf("%d x %d = %d \n", &mult, &i , &ans);
}
return 0;
}
Hi guys, this is a simple code which is supposed to help the user to list the times table for n times. However, i am receiving undefined behaviour and I am quite stumped as to what is wrong with my implementation of my "for" loop.
I am receiving this as my output.
6356744 x 6356748 = 6356736
for n times in my consoles.
I want to ask
Is anything wrong with the logic of my code? (i assume i do have a problem with my code so please do enlighten me)
Would it be better(or even possible) to use pointers to point to the memory addresses of the mentioned variables when i have to change the value of the variables constantly? If yes, how do i go around doing it?
Thanks!
In printf you must provide integers. You are now giving the addresses of integers. So change
printf("%d x %d = %d \n", &mult, &i , &ans);
to
printf("%d x %d = %d \n", mult, i, ans);
and to make the table, replace ans with just mult*i, so:
printf("%d x %d = %d \n", mult, i, mult*i);
You should also check the return value of scanf to check if it has succeeded reading your input:
do {
printf("Please enter a multiple you want to explore.");
} while (scanf("%d", &mult)!=1);
do {
printf("Please enter the number which you would want to multiply this number till.");
} while (scanf("%d", &n)!=1);
The things you see are the values of the variables memory location.
Change your lines inside for loop as below
ans = mult * i;
printf("%d x %d = %d \n", mult, i, ans);
There are some mistakes in your code .
you are using the & operator in print statement which is used to print the address of the variable.
Initiate the loop with the value '1' instead of '0' & execute the loop till 'i' less than equal to 'n'.
instead of using the ans variable outside the loop , use it inside the loop as it evaluate the multiplication result in each iteration of the loop.
#include <stdio.h>
int main()
{
int i;
int mult;
int n;
int ans;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 1; i<=n; i++) {
ans = mult*i ;
printf("%d x %d = %d \n", mult, i , ans);
}
return 0;
}
I have prompted the user for 5 inputs in the main and saved them into an array called array. I've checked the array in main and it prints out the values as they were entered. However, when I pass it to a function which I've included below, I get the output that the array contains all 0 values.
What am I doing wrong?
conversion(float array[], int size)
{
float add = 0.0;
float change, num;
printf("\nThe array is: \n");
for (i=0;i < size;i++)
{
printf("%.2f\n",&array[i]);
}
/*calculate and store the conversion values in a new array*/
for(i=0; i<s; i++)
{
num = array[i];
change = (num*100.50);
for(j=0; j<1; j++)
{
printf("\n %.2f your number is %.2f in float percent\n", &num, &change);
}
}
}
&cArray[i]
you don't need to address of the ith element in order to print it, you just need the ith element
Change
printf("%.2f\n",&cArray[i]);
printf("\n %.2f in Celsius is %.2f in Fahrenheit\n", &temp, &con);
to
printf("%.2f\n",cArray[i]);
printf("\n %.2f in Celsius is %.2f in Fahrenheit\n", temp, con);
The same with con, printf() doesn't need the address of a scalar variable in order to print it.
Like Johnny Mopp said you have an extraneous &,
But depending on the size of the array you're dealing with you may just want to pass a pointer to the function instead.
Observe the following code. I want to divide the amount by the value of the last element in the array. I have tried in the following way but it is not working. Can anyone tell me what is the proper way to do it?
#include<stdio.h>
int main()
{
int i, j, k, noteNumber, array[100], amount, result;
printf("Enter the number of the notes: \n");
scanf("%d", ¬eNumber);
printf("Enter the value of %d notes: \n", noteNumber);
for(i = 0; i < noteNumber; i++){
scanf("%d", &array[i]);
}
printf("Enter the amount: \n");
scanf("%d", &amount);
i = j;
if(amount / array[j] == 0){
printf("Minimum %d number of is needed", (amount/array[j]));
printf("The value of each note is %d", array[j]);
}
}
As I can see
i = j;
is wrong as you're using the value of an uninitialized variable to assign to another. This does not make any sense and can lead to undefined behavior.
C arrays use 0-based indexing, so for an array of size n, the last index would be n-1.
That said, never use an unbound index for a statically defined array, always perform the bound checking before using the index.
If noteNumber is the size of the array, then the last Element will be
array[noteNumber - 1]
As far as I can see, j isn't even initialized?
You are having a line
i = j;
j is not even initialized so you are doing a mistake here , maybe what
you wanted was
j = i - 1
As i would have incremented to noteNumber in your for loop and array with number of elements n has last element index n-1 because index starts from 0 rather than 1.
So Proper Code Would Be
#include<stdio.h>
int main(){
int i, j, k, noteNumber, array[100], amount, result;
printf("Enter the number of the notes: \n");
scanf("%d", ¬eNumber);
printf("Enter the value of %d notes: \n", noteNumber);
for(i = 0; i < noteNumber; i++){
scanf("%d", &array[i]);
}
printf("Enter the amount: \n");
scanf("%d", &amount);
j = i - 1; // Line Changed
if(amount / array[j] == 0){
printf("Minimum %d number of is needed", (amount/array[j]));
printf("The value of each note is %d", array[j]);
}
}
scanf("%1c%2c %d %d %d %d %d %d %d %d %d %d",
&x, &y, &arr[0], &arr[1], &arr[2], &arr[3], &arr[4],
&arr[5], &arr[6], &arr[7], &arr[8], &arr[9]);
strcpy(string, x);
value1 = atoi(string);
strcpy(string, y);
value2 = atoi(string);
value_final = value1 + value2;
I'm trying to get the ascii values for the -l or -s then pass them through the switch with the ascii values added but I'm getting errors when I use atoi and I'm not sure as to if you are supposed to be adding the ascii values when the user enters the -l or -s or if their is another way to do this?
Your code has several problems:
Major: the switch cases do not have a break; clause. Control falls into the next clause and finally into the default statement.
Major: your show confusion between char arrays and single char variables: strcpy(string, x); should not even compile.
Your method for parsing -l or -s is very convoluted and probably erroneous. You should use character literals.
You do not need a double loop to find the smallest or the largest element in an array. A single loop suffices, and the printf statement should be outside the loop. max and min are uninitialized, the loops invoke undefined behavior.
the title says using atoi(): if this was your assignment, you should not use scanf().
Here is a simplified version:
int main(void) {
char option[3], buffer[32];
int i, min, max, value;
min = max = 0;
if (scanf("%2s", option) == 1) {
for (int i = 0; i < 10 && scanf("%31s", buffer) == 1; i++) {
value = atoi(buffer);
if (i == 0) {
min = max = value;
} else {
if (min > value)
min = value;
if (max < value)
max = value;
}
}
if (!strcmp(option, "-l")) {
printf("output: The largest number is %d\n", max);
return 0;
} else
if (!strcmp(option, "-s")) {
printf("output: The smallest number is %d\n", min);
return 0;
}
}
printf("You have entered an invalid option, try again next time.\n");
return 0;
}