Can some help me and explain where i got it wrong?
I just don't know where I'm wrong;C
void largest(int);
void smallest(int);
void average(double);
int main()
{
int n=0,i=1,num,max=0,min=0,sum=0;
double avg=0;
cout<<"Please enter total number of integers: ";
cin>>n;
cout<<"\n";
while (n>0)
{
cout<<"Enter integer "<<i<<": ";
cin>>num;
if (num>max)
max=num;
if (num<min)
min=num;
sum=sum+num;
n--;
i++;
}
avg=sum/n;
largest(max);
smallest(min);
average(avg);
return 0;
}
void largest(int max)
{
cout<<"The largest integer is: "<<max;
}
void smallest(int min)
{
cout<<"The smallest integer is: "<<min;
}
void average(double avg)
{
cout<<"The average is: "<<avg;
}
From my understanding im not so sure if this is correct but i need to use void to print out a message and im a bit confuse as to when i should use void and what does the difference between double& and double when i use it?
Process exited ... with return value 3221225620
3221225620 is the numeric code for a STATUS_INTEGER_DIVIDE_BY_ZERO (0xC0000094) exception, which means your code is crashing before it has a chance to print out its messages.
On this line:
avg=sum/n;
n is 0 at this point, because the while loop above it had decremented n on each iteration until n reached 0.
To avoid that, change the while loop to not modify n anymore:
while (i <= n)
{
...
i++;
}
Or, use a for loop instead:
for (int i = 1; i <= n; ++i)
{
...
}
you're dividing by 0, since you're modifying n until it reaches 0 and then use it to divide, it's better if you iterate with another variable instead of reducing the value of n. so the problem isn't that it's not printing, it's that the program dies before reaching that
Related
here's the code i made for finding jumping numbers less than or equal to a given no. it shows error "Output Limit Exceeded"
int main() {
int t;
cin>>t;
while(t--)
{
long long int n ,rem,temp;
cin>>n ;
for(int i=0 ; i<=n ; ++i)
{
if(i<=10)
cout<<i<<" ";
else
{
temp=i;
do
{
rem=i%10;
i/=10;
}while(abs(rem-i%10)==1);
if(i==0)
{
cout<<temp<<" "; // printing jumping no.s
}
}
}
cout<<"\n";
}
return 0;
}
You're dividing i by 10 in your do-while loop! As soon as i hits 11, it will be divided by 10, and then at the next iteration of the main for loop, you'll have a very low i value, back to where it was earlier. You're loop will never end.
void base(int n,int b)
{
int x[20],k=0;
while(n!=0)
{
x[k]=n%b;
n=n/b;
k=k+1;
}
for(int i=0;i<=k;i++)
{
cout<<x[k-i];
}
return ;
}
int main()
{
int number,baise;
cin>>number;
cin>>baise;
base(number,baise);
return 0;
}
It is program of base expansion. I wrote a function for that and in main() I call it with 2 parameters (that I want to convert into another base) and baise (that is a base value into which a number is converted). The program runs and it also gives output but with the input it gives some undesirable values, which is 6295648 in this case.
Input:
241
2
Output
629564811110001
But the output should be 11110001 only. I don't understand why the value 6295648 comes.
for(int i=0;i<=k;i++)
{
cout<<x[k-i];
}
At the first iteration of the loop, the executed statement is:
cout << x[k];
where x[k] is never assigned, which is an UB.
Changing the loop initialization statement to x = 1 solves this problem. Or more clearly,
for(int i = k-1; i >= 0; i--)
{
cout << x[i];
}
The issue with your code is that k gets incremented post the assignment in the x[] array i.e. In the last iteration k gets incremented to 8. Then in case of k-i when the value of i is 0 .. x[k-i] goes out of bounds.
#include <iostream>
using namespace std;
void base(int n,int b)
{
int x[20],k=-1;
while(n!=0)
{
x[++k]=n%b; //pre increment
n=n/b;
}
for(int i=0;i<=k; i++)//now k contains the index of the last element
{
cout<<x[k-i];
}
return ;
}
int main()
{
int number,baise;
cin>>number;
cin>>baise;
base(number,baise);
return 0;
}
I am trying to input 10 numbers and then call the function(sorting) to sort them in ascending order. After sorting it, the main program will also call the function getAvg to average the numbers in the array. However, after putting 10 values into the array. There are some random numbers displayed before my output.
What am I doing wrong?
#include<iostream>
#include<stdio.h>
using namespace std;
void sorting(int array[]) {
int t;
int x=0;
for (x=0; x<10; x++)
{
for (int y=0; y<9; y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
}
double getAvg(int array[]) {
double sum=0;
double avg=0;
for (int j=0;j<10;j++){
sum=sum+array[j];
}
avg=sum/10;
return avg;
}
int main()
{
int input=0;
int array[10];
double avg=0;
printf("%s","Enter the 10 temperatures \n");
for(int i=0;i<10;i++){
scanf("%i",&input);
array[i]=input;
}
sorting(array);
avg = getAvg(array);
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
printf("%s %.2lf %s","The average is ",avg, ".");
}
I think the error is in this code:
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
Notice that on the first iteration of the loop, you'll have k=0, so this tries to print out array index -1. This results in undefined behavior - technically speaking, anything can happen - and in your case it's reading garbage data from before the start of the array.
To fix this, change the loop bounds so that they properly range over the array:
for (int k=0; k < 10;k++){
cout<<array[k]<<" ";
}
My guess is that you realized that you were reading too far and tried to fix this by subtracting one from the array index, which just introduced a new bug. Hopefully this corrects this!
for (int k=0;k<=10;k++){
cout<<array[k-1]<<" ";
}
You are showing from -1 to 9, which means 11 values. The 1st value (array[-1]) was not initialized.
You need to change the k<=10 by k<10 and print array[k] instead of array[k-1]:
for (int k=0;k<10;k++){
cout<<array[k]<<" ";
}
The problem is that you are printing the elements at positions -1 through 9 of your array. There is no position -1.
i was asked to create a recursive function that outputs the minimum number of the array that the user enters. the problem is when run the code the output is funny and the cout is repeated many times with weird values.
#include <iostream>
using namespace std;
void recursiveMinimum(int i, int f, int *a, int min)
{
if(a[i]<min)
{
min=a[i];
}
i++;
if (i<f)
{
recursiveMinimum(i, f, a, min);
}
cout<<"the minimum number is "<<min<<endl;
}
void main ()
{
int *a, b=1000, f;
a= new int [b];
cout<<"please enter the array, enter '1000' without the quotes to stop"<<endl;
for(int i=0; i<b; i++)
{
cin>>a[i];
if (a[i]==1000)
{
a[i]=NULL;
f=i;
break;
}
}
recursiveMinimum(0, f, a, a[0]);
system("pause");
}
I don't know C++ very well, but doesn't cout get called many times, specifically each time the recursive function gets called? Here is the updated recursiveMinimum function:
void recursiveMinimum(int i, int f, int *a, int min)
{
if(a[i]<min)
{
min=a[i];
}
i++;
if (i<f)
{
recursiveMinimum(i, f, a, min);
} else {
cout<<"the minimum number is "<<min<<endl;
return;
}
}
I moved the print statement into an else statement. When I isn't less than F, which should only happen when you have reached the end of the array, the program will print the minimum number and end.
I can't get the sum to print out
I did not get any warnings or such.
Trying to do this question here:http://projecteuler.net/problem=1
the program runs and then just stalls forever.
// Adding multiples of this and following equations
#include "std_lib_cmpt125.h"
void SumMultiple(int Max,int Base)
{
int i=0;
int sum=0;
for(i;i<Max;i+Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
}
int main()
{
int base=0;
int max=0;
int sum=0;
cout<<"Please enter the sum's multiple: (ex. 3: 3,6,9,12...)\n";
cin>>base;
cout<<"Please enter the sum's maximum: (ex. 10000)\n";
cin>>max;
SumMultiple(max,base);
return 0;
}
}
You missed to increment the i,
for(i;i<Max;i=i+Base)
{
..
}
Or
for(i;i<Max;i+=Base)
{
..
}
Change:
int i=0;
int sum=0;
for(i;i<Max;i+Base)
{
sum=i+sum;
};
To:
int sum = 0;
for (int i = 0; i < Max; i += Base)
{
sum += i;
}
Note that the first statement in the original for loop does nothing, that the value of i must be incremented (which is accomplished most simply with the += operator), and that the semicolon after the closing brace is unnecessary.
It's customary in C++ to declare a loop variable in the loop itself, to limit its scope and emphasize its purpose as a counter.
You have an extra Brace } at the end and you are not incrementing i
Change the
for(i;i<Max;i+Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
into
for(i;i<Max;i=i+Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
or
for(i;i<Max;i+=Base)
{
sum=i+sum;
};
cout<<"The sum"<<sum;
The above code will increment the counter i and display the necessary result
Also you aren't going to get the answer with that function. problem 1 is looking for the sum of all multiples of 3 or 5 from [1..999]
try this
for ( int i = 1; i < Max ; i++ )
sum += ((i % 3 && i % 5) ? 0 : i ;
return sum