-1

i need a program that takes input numbers and print stars as many as the numbers count then sort the numbers decreasing. all the functions have to be in a header file.

#include <iostream>
using namespace std;

 int read(int a[],int n)
 {
     int i=0;
     cout<<"please enter numbers: ";
     do
        cin>>a[i];
     while (a[i++]>-1 && i<n);
     return (i-1);
 }
 void print(int const a[], int const n)
 {
     for(int i=0;i<n;i++)
     cout<<a[i]<<"* ";
     cout<<endl;
 }
 void sort(int a[], int const n)
 {
     for (int i=1; i<n;i++)
        for(int j=0;j<n-i;j++)
        if(a[j]>a[j+1]) swap (a[j],a[j+1]);
 }

this is as far as i got but i don't know how to print the stars and to use the header in the main program.

  • 1
    I suspect there are no method being called. As what I remember, c++ calls the `main` method when the program runs, https://www.w3schools.com/cpp/cpp_getstarted.asp – Forbidden Dec 29 '21 at 06:31
  • Why should the functions be in a header file? Unless you make the function `inline`, function bodies belong in the implementation file, not the header. To me, all you need to do is add `main` at the bottom of the code you posted, and forget about header files (except for the ones required such as ``, ``, etc.) – PaulMcKenzie Dec 29 '21 at 07:33

2 Answers2

0

Here is how you can add your program to the main program . Save your program as a header file with .h extension . And include in your program as #include yourfilename.h here I assume your filename as program.

    #include "program.h" // Here you are adding your program as a hadder.save your program with as program.h file
    #include <iostream>
    using namespace std;
    int main(){
      int a[100]; // define a array you can change the length of array also
      read(a,10); // calling the read function of your program a is the array a[] and 10 is the length of array or n
      sort(a,10);//calling the sort function of your program a is the array a[] and 10 is the length of array or n
      print(a,10);//calling the print function of your program a is the array a[] and 10 is the length of array or n
    }
  • thanks for your help. but what if i don't want to set the length of the array and it takes the length as the input and do the rest of the stuff. – mohamadreza jafarzadeh Dec 29 '21 at 07:07
  • @mohamadrezajafarzadeh: `#include ... int length; ... std::vector a(length);`. Arrays cannot be resized, that's why vector exists. – PaulMcKenzie Dec 29 '21 at 07:15
  • yes that's true that's why i used const number for it.is there any other way to do it without a vector? – mohamadreza jafarzadeh Dec 29 '21 at 07:19
  • @mohamadrezajafarzadeh -- What's wrong with using vector, something that has been in standard C++ for 24 years now? – PaulMcKenzie Dec 29 '21 at 07:21
  • `int n; cout<<"Length of array"; cin>>n; int a[n];` this is how you can set the length of array – Masudur Rahman Sourav Dec 29 '21 at 07:21
  • @MasudurRahmanSourav -- No. That is not valid C++. Arrays in C++ must have their size denoted by a compile-time expression, not a runtime value. – PaulMcKenzie Dec 29 '21 at 07:22
  • @PaulMcKenzie you can try I solved tons of problem using this – Masudur Rahman Sourav Dec 29 '21 at 07:23
  • @MasudurRahmanSourav -- Please [read this](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Variable length arrays are *not* standard C++. Steering the new programmer to use non-standard C++ syntax should be discouraged. Also, that code will refuse to be compiled using Visual C++. Also [see ths](https://godbolt.org/z/9d1175acM). – PaulMcKenzie Dec 29 '21 at 07:24
  • @PaulMcKenzie Thanks for your information . I didn't know about the standard. It's worked fine for me but thank you again for the information – Masudur Rahman Sourav Dec 29 '21 at 07:28
0

this is the answer i was looking for i post it here maybe used by someone else.

#include <iostream>

using namespace std;

int get_numbers(int a[]) {
    int i = 0;
    while (a[i] >= 0) {
        cout<<"please enter a number: ";
        cin >> a[i];
        if (a[i] < 0)
            break;
        i++;

    }
    return i;
}

void print_stars(int n) {
    for (int i = 0; i < n; i++) {
        cout << "* ";
    }
    cout << endl;
}

void sort(int a[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (a[j] < a[j + 1]) {
                int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

void print_sorted_numbers(int a[], int n) {
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
}