recursive function to outputs the minimum number of the array - c++

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.

Related

Why is it not printing the coding of the void functions?

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

What is the error in this bubble sort code?

I wrote this simple code for bubble sort but it gives some random garbage values as output. Can someone please tell me my mistake. I tried to print the output of A[i] and A[j] in the function bubbleSort and looks like it is working fine. But why is the printSortedArray not giving the correct output? Thanks!
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void printSortedArray(int A[],int size)
{
cout<<"the sorted array is"<<endl;
int i;
for(i=0;i<size;i++);
{
cout<<A[i]<<" ";
}
}
void bubbleSort(int A[],int size)
{
int i,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-1-i;j++)
{
if(A[j]>A[j+1])
{
swap(A[j],A[j+1]);
}
}
}
}
int main()
{
int A[50]; int size,i;
cout<<"enter the size of the array: ";
cin>>size;
cout<<"Enter the "<<size<<" numbers to be sorted"<<endl;
for(i=0;i<size;i++)
{
cin>>A[i];
}
bubbleSort(A,size);
printSortedArray(A,size);
return 0;
}
for(i=0;i<size;i++);
The trailing semicolon does not belong there. This results in undefined behavior.
The end result is that this function printed one garbage value after the end of the array.

Finding the sum of n big numbers

Question: Given a number k, find the sum of k positive big integers.
This is my code,it works, but our Online Judge is rejecting it, says segfault. Why does it show segfault? I could do it with two strings but why isn't this working?
#include <iostream>
#include <string.h>
using namespace std;
void add(int l,int k);
void append(char a[], int temp);
int o;
int tf=0;
int carry=0;
char b[1000000];
char a[10000][10000];
char c[1000000];
int main()
{
int k,x=0,l=0,m=0;
cin>>k;
while(x<k)
{
cin>>a[x];
if(strlen(a[x])>l)
{
l=strlen(a[x]);
}
x++;
}
x=0;
while(x<k)
{
if(strlen(a[x])<l)
{
int temp=0;
append(a[x],l-strlen(a[x]));
}
x++;
}
add(l,k);
if(carry!=0)
{
cout<<carry;
}
while(o>=0)
{
cout<<(int)b[o];
o--;
}
}
void add(int l,int k)
{
int lb=l-1;
int r=k-1;
int sum=0;
int x=0;
int neg=0;
while(lb>=0)
{
r=k-1;
sum=0;
while(r>=0)
{
sum=sum+a[r][lb]-48;
r--;
}
sum=sum+carry;
lb--;
if(sum>=10)
{
b[x]=sum%10;
carry=sum/10;
}
else
{
b[x]=sum;
carry=0;
}
sum=0;
o=x;
x++;
}
}
void append(char a[], int temp)
{
int l=0,m;
int tempb=temp;
m=strlen(a)-1;
while(temp>0)
{
c[l]='0';
l++;
temp--;
}
int z=0;
while(z<=m)
{
c[l]=a[z];
z++;
l++;
}
z=0;
while(z<=m+tempb)
{
a[z]=c[z];
z++;
}
}
Input Format:
First line contains k , which specifies the number of big numbers. Each of the next k lines contains a
big positive integer.
Output Format:
For each test case print the new big integer in a new line
Sample Input:
3
1331331
1313
453535322
Sample Output:
454867966
Constraints:
1<=k<=10
1<=number of digits in big numbers<=10000
Based on the problem statement:
Each input entry my have at most nmax = 10000 digits
Since each entry is stored as a C-style, zero-terminated string, each character array must have a length of (nmax + 1) = 10001 characters, to accomodate for the C-string terminator '\0'.
As you stored the entries into character arrays without leaving room for the zero terminator, assuming each entry was 10000-characters long:
Each entry with k>=1 overwrote the terminator or entry k-1, thus coalescing the entries together;
You thus ended with one giant string, with l = strlen(a[0]) = 100000;
From then on, all further processings were performed with these incorrect (coalesced) inputs and length, leading to buffer overrun at some point later in the execution.

Sorting an array from lowest to greatest

So i'm in my first programming class and am having some trouble with an assignment. The assignment was to make a code that asks the user for six integers and six letters and to store them in arrays. Then print a bar graph with the data sorted from lowest to highest. That is the part i am having trouble with. I named it the Arrange function, and to me everything looks right. I was just hoping someone could tell me where i went wrong in swapping the lowest integer. Any other comments on the code would be much appreciated! Thanks (I also cant use sort functions from a library, or pointers)
#include <iostream>
using namespace std;
void Arrange(int num[],int order[],char let[],char letOrder[],int cap);
void Swap(int order[],char let[],int a,int b);
void BuildGraph(int number[],char letter[],int max);
int getMax(int number[]);
int main(){
int Number[6];
char Letter[6];
int Order[6];
char LetterOrder[6];
int max;
cout<<"Please Enter 6 Integers: "; //gets 6 integers
for(int i=0;i<6;i++){
cout<<"Ineteger # "<<i+1<<" = ";
cin>>Number[i];
cout<<endl;
}
cout<<"Please Enter 6 Characters: "; //gets 6 characters
for(int i=0;i<6;i++){
cout<<"Enter a character here: ";
cin>>Letter[i];
cout<<endl;
}
max= getMax(Number);
BuildGraph(Number,Letter,max);
Arrange(Number,Order,Letter,LetterOrder,max);
BuildGraph(Order,LetterOrder,max);
return 0;
}
int getMax(int number[]){
int max=number[0];
for(int i=0;i<6;i++){
if(max<number[i]) max=number[i];
}
return max;
}
void Arrange(int num[],int order[],char let[],char letOrder[],int cap){
int i,n;
order=num;
letOrder=let;
for(i=0;i<5;i++){
for(n=0;n<6;n++){
if(order[i]>order[n]){
Swap(order,letOrder,i,n);
}
}
}
cout<<order[4];
}
void Swap(int order[],char let[],int a,int b){
int temp1=order[b];
order[b]=order[a];
order[a]=temp1;
int temp2=let[b];
let[b]=let[a];
let [a]=temp2;
}
void BuildGraph(int number[],char letter[],int max){
cout<<"Bar Chart"<<endl;
for(int row=max;row>=1;row--){
for(int col=0;col<6;col++){
if(row<=number[col]) cout<<letter[col]<<" ";
else cout<<" ";
}
cout<<endl;
}
}
It would probably be easier to visualize and implement if you have separate functions for arrange and swap.
void arrangeNum(int num[])
void arrangeChar(char char[])
void swapInt(int x, int y)
void swapChar(char x, char y)
Then the arrange functions will just be bubble sorts. But you also lose scope when calling swap so they don't actually do anything. It would be easier to just swap them within the Arrange functions.

Function returns seemingly random number instead of the value of returned variable

So, I'm writing this simple code (when finished, it should count the values of n polynomials, each of r degree, in n points. I also cannot use loops, I ought to do the stuff in recursive functions - but it's not done yet and the topic has nothing to do with my problem).
So, the code so far is:
#include <iostream>
#include <cmath>
using namespace std;
void wypisz(int i, int k, int* c)
{
if (i<k) cout<<*(c+i)<<" ";
else return;
i++;
wypisz(i,k,c);
}
void podaj(int i, int k, int* c)
{
if (i<k) cin>>*(c+i);
else return;
i++;
podaj(i,k,c);
}
int wynik (int i, int k, int* c, int su, int mn)
{
int y;
if (i<k) {
cin>>y;
su+=y*mn;
mn*=*c;
}
else {cout<<su<<endl; return su;}
i++;
wynik (i, k, c, su, mn);
}
int main(){
int m,n,r;
cin>>m;
int tab[m];
podaj(0,m,tab);
wypisz(0,m,tab);
cin>>r;
cout<<wynik(0,r,tab,0,1);
system("PAUSE");
return 0;
}
It counts the first polynomial in first point well, but only in function. When calling
cout<<su;
just before terminating the wynik() function, it gives correct value, but when calling
cout<<wynik(0,r,tab,0,1);
in main(), it gives much higher, seemingly random value.
Could anyone tell me why?
Thanks in advance :)
In your wynik function, when doing recursion, you aren't exacly returning the calculated value... you should change the last line from:
wynik (i, k, c, su, mn);
to
return wynik(i, k, c, su, mn);
Or, even better, change the whole function so it looks clear:
int wynik (int i, int k, int* c, int su, int mn)
{
int y;
if (i<k) {
cin>>y;
su+=y*mn;
mn*=*c;
return wynik (++i, k, c, su, mn);
}
else return su;
}