Jump to content
In the Name of God بسم الله

Recommended Posts

  • Advanced Member
Posted

(salam)

I know this should be in the off-topic section, but I wanted to get some attention.  I've written a program for my c++ class for an IIR filter and I'm having problem with it.  my output is coming out wrong when I use a function for the math part.  I've written the program without using a separate function and I get the output right, but when I use a function it compiles, but the output comes out wrong!.  People said I need to use a pointer, but I don't know how!  Please help anyone!

I use Dev C compiler, this is the program I wrote:

#include < iostream.h > \\no spaces here, for some reason anything in "<" ">" doesn't show up

#include < stdlib.h >

void iir(int numpoints, double input[], double output[], double alph)

{

    input[0]=output[0];

    for (int i = 1; i < numpoints; i++)

        output = alph*input + alph*output[i-1];

    return;

}

int main()

{

   double alpha, x[50], y[50];

   cout << "Enter Alpha: ";

   cin >> alpha;

   if (alpha <= 0)

   {

       cout << "Alpha must be larger then zero! \n";

       system("pause");

       return 0;

   }

   if (alpha >= 1)

   {

       cout << "Alpha must be less then 1! \n";

       system("pause");

       return 0;

   }

   int numValues;

   cout << "Enter number of values: ";

   cin >> numValues;

   for (int i = 0; i < numValues; i++)

   {    

       cout << "Enter a value: ";

       cin >> x;

   }

   iir (numValues, x, y, alpha);

   for (int i = 0; i < numValues; i++)

       cout <<  y << endl;

   

    system("PAUSE");

    return 0;

}

Edited By Aamir on 1034387816

  • Site Administrators
Posted

Yeah I have to agree.  The ShiaChat tech is the mastermind behind all this.. He'll probably help you out.

All I know in C++ is

cout

Edited By Ali on 1034389107

  • Advanced Member
Posted
(salam)

I know this should be in the off-topic section, but I wanted to get some attention.  I've written a program for my c++ class for an IIR filter and I'm having problem with it.  my output is coming out wrong when I use a function for the math part.  I've written the program without using a separate function and I get the output right, but when I use a function it compiles, but the output comes out wrong!.  People said I need to use a pointer, but I don't know how!  Please help anyone!

I use Dev C compiler, this is the program I wrote:

#include < iostream.h > \\no spaces here, for some reason anything in "<" ">" doesn't show up

#include < stdlib.h >

void iir(int numpoints, double input[], double output[], double alph)

{

    input[0]=output[0];

    for (int i = 1; i < numpoints; i++)

        output = alph*input + alph*output[i-1];

    return;

}

int main()

{

   double alpha, x[50], y[50];

   cout << "Enter Alpha: ";

   cin >> alpha;

   if (alpha <= 0)

   {

       cout << "Alpha must be larger then zero! \n";

       system("pause");

       return 0;

   }

   if (alpha >= 1)

   {

       cout << "Alpha must be less then 1! \n";

       system("pause");

       return 0;

   }

   int numValues;

   cout << "Enter number of values: ";

   cin >> numValues;

   for (int i = 0; i < numValues; i++)

   {    

       cout << "Enter a value: ";

       cin >> x;

   }

   [b:post_uid0]iir (numValues, &x, &y, alpha);[/b:post_uid0]

   for (int i = 0; i < numValues; i++)

       cout <<  y << endl;

   

    system("PAUSE");

    return 0;

}

  • Site Administrators
Posted

:o i've been summoned...lol

Salamz bro

i'm not sure exactly what u're trying to do :s

i compiled ur program, and it didn't work at first, error i got was "redefinition, multiple initializations", that pertained to the i decleration you made in this for loop

for (int i = 0; i

      cout

changed the i's to any other name, and it worked.

Enter Alpha: 0.1

Enter number of values: 5

Enter a value: 5

Enter a value: 10

Enter a value: 15

Enter a value: 20

Enter a value: 25

-9.25596e+061

-9.25596e+060

-9.25596e+059

-9.25596e+058

-9.25596e+057

i'm not sure if that's the output u want :s

wassalamz

P.S. bro, i live in australia, and we can't but loose it at the pakistani cricket team. Australia got them all out for 59  :o  :o  :o holy cow. It was the lowest ever pakistani test score!!! what's goin on bro??? ;)

check out my little buddy

AUSSIE AUSSIE AUSSIE...OY OY OY

image

  • Site Administrators
Posted

salamz

bro dhulfiqars version does the trick also

although i had to change

iir (numValues, &x, &y, alpha);

to

iir (numValues, &x[50], &y[50], alpha);

for it work  :o  that could be just a Visual C++ thing, i don't know :s

n e wazy, the output i got was

Enter Alpha: 0.1

Enter number of values: 5

Enter a value: 5

Enter a value: 10

Enter a value: 15

Enter a value: 20

Enter a value: 25

-9.25596e+061

-9.25596e+061

-9.25596e+061

-9.25596e+061

-9.25596e+061

:s wassalamz

  • Advanced Member
Posted

thanks a lot you guys, I figured out what I did wrong, it was the "return;" I put for the first function that was screwing up my entire program!  i really appreciated the help though because of the damn pointers I still have to figure out how to use.

P.S. bro, i live in australia, and we can't but loose it at the pakistani cricket team. Australia got them all out for 59       holy cow. It was the lowest ever pakistani test score!!! what's goin on bro???

check out my little buddy

What!  I can't beleive this!  They probably got some "extra benefits" for loosing that game.  :(

Enter Alpha: 0.1

Enter number of values: 5

Enter a value: 5

Enter a value: 10

Enter a value: 15

Enter a value: 20

Enter a value: 25

-9.25596e+061

-9.25596e+061

-9.25596e+061

-9.25596e+061

-9.25596e+061

Just out of curiousity, do you guys know how to convert this number -9.25596e+061 to a normal one?

Edited By Aamir on 1034431361

  • Advanced Member
Posted

it was just a program my prof said to make.  He gave us the equation for the digital filter, and we were suppose to just make a program that calculates the results.  This is for an intro to c++ class, so it was pretty easy, I just made a small mistake.

Whats dsp?

  • Advanced Member
Posted
IME LOST !!!

lol, don't worry about it sis.  I'm guessing you won't be an engineering major right.  There are barely any women computer/electrical engineering majors in my school.  probably around 8 out of 60 would be the ratio.

  • Advanced Member
Posted

lol, thanks  dhulfiqars for the tips.  

But i shud say whoever studied puter science kinda lost his/her mind....    

lol, they take the easy way out.

  • Advanced Member
Posted

Salam Alaykum,

Anyone who can help me with my Java programming?

I'll post 2 classes, the second class is called by the first class and etc...

The output that I should get should be similar to this:

3/4 / 4/2 = 3/8

3/4 * 1/2 = 3/8

3/4 - 1/2 = 1/4

3/4 + 1/2 = 5/4

But instead I get the output:

Usage: java TestCommandParameters operator ratonal1 rational2

The original class which the output should print from it is:

import java.util.StringTokenizer;

public class StringToken

{

public static void main(String[] args)

{

//The result of the operation

Rational result = new Rational();

if(args.length != 3)

{

System.out.println(

"Usage: java TestCommandParameters " +

"operator ratonal1 rational2" );

System.exit(0);

}

Rational r1 = stringtoRational(args[1]);

Rational r2 = stringtoRational(args[2]);

String s = args[0];

//Determine the operator

switch (args[0].charAt(0))

{

case '+': result = r1.add(r2);

 break;

case '-': result = r1.subtract(r2);

 break;

case '*': result = r1.multiply(r2);

 break;

case '/': result = r1.divide(r2);

}

//Display result

System.out.println(r1.toString() + " "+ s + " " + r2.toString() + " = " +

  result.toString());

}

public static Rational stringtoRational(String s)

{

StringTokenizer st = new StringTokenizer(s, "/" );

long num = Long.parseLong(st.nextToken());

long denom = Long.parseLong(st.nextToken());

Rational r = new Rational(num, denom);

return r;

}

}

************************

public class Rational

{

 // Data fields for numerator and denominator

 private long numerator = 0;

 private long denominator = 1;

 // Default constructor

 public Rational()

 {

   numerator=0;

denominator=1;

 }

 

 // Construct a rational with specified numerator and denominator

 public Rational(long n, long d)

 {

   long k = gcd(n,d);

   numerator = n/k;

   denominator = d/k;

 }

 // Find GCD of two numbers

 private long gcd(long n, long d)

 {

   long t1 = Math.abs(n);

   long t2 = Math.abs(d);

   long remainder = t1%t2;

   while (remainder != 0)

   {

     t1 = t2;

     t2 = remainder;

     remainder = t1%t2;

   }

return t2;

 }

 // Getter method for numerator

 public long getNumerator()

 {

   return numerator;

 }

 public long getDenominator()

 {

   return denominator;

 }

 // Add a rational number to this rational

 public Rational add(Rational secondRational)

 {

   long n = numerator*secondRational.getDenominator() +

     denominator*secondRational.getNumerator();

   long d = denominator*secondRational.getDenominator();

   return new Rational(n, d);

 }

 // Subtract a rational number from this rational

 public Rational subtract(Rational secondRational)

 {

   long n = numerator*secondRational.getDenominator()

     - denominator*secondRational.getNumerator();

   long d = denominator*secondRational.getDenominator();

   return new Rational(n, d);

 }

 // Multiply a rational number to this rational

 public Rational multiply(Rational secondRational)

 {

   long n = numerator*secondRational.getNumerator();

   long d = denominator*secondRational.getDenominator();

   return new Rational(n, d);

 }

 // Divide a rational number from this rational

 public Rational divide(Rational secondRational)

 {

   long n = numerator*secondRational.getDenominator();

   long d = denominator*secondRational.numerator;

   return new Rational(n, d);

 }

 // Override the toString() method

 public String toString()

 {

   return numerator + "/" + denominator;

 }

 

 //Returns true if the rational is < r

 public boolean lessThan(Rational r)

 {

double retn1 = (double)numerator / (double)denominator;

double retn2 = (double)r.getNumerator() / (double)r.getDenominator();

if (retn1 < retn2)

return true;

else

return false;

 }

  //Returns true if the rational is > r

  public boolean greaterThan(Rational r)

{

double retn1 = (double)numerator / (double)denominator;

double retn2 = (double)r.getNumerator() / (double)r.getDenominator();

if (retn1 > retn2)

return true;

else

return false;

}

//Returns true if the rational is = r

public boolean equal(Rational r)

{

double retn1 = (double)numerator / (double)denominator;

double retn2 = (double)r.getNumerator() / (double)r.getDenominator();

if (retn1 == retn2)

return true;

else

return false;

}

//Returns true if the rational is <= r

public boolean lessThanOrEqual(Rational r)

{

double retn1 = (double)numerator / (double)denominator;

double retn2 = (double)r.getNumerator() / (double)r.getDenominator();

if (retn1 <= retn2)

return true;

else

return false;

}

//Returns true if the rational is >= r

public boolean greaterThanOrEqual(Rational r)

{

double retn1 = (double)numerator / (double)denominator;

double retn2 = (double)r.getNumerator() / (double)r.getDenominator();

if (retn1 >= retn2)

return true;

else

return false;

}

//Returns the larger rational number

public Rational max(Rational r)

{

double retn1 = (double)numerator / (double)denominator;

double retn2 = (double)r.getNumerator() / (double)r.getDenominator();

if (retn1 < retn2)

return r;

else

return new Rational(numerator,denominator);

}

}

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...