By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

Why?

Savage
Expert 100+
P: 1,764
Hi,

I'm making a programme that finds equivalent resistance between 3 resistors.It also asks user how to connect those resistors.I decided to write a function

float count(float r[],int type) which will calculate resistance in depandance of parametar type.

The problem is that whataver resistances user input in main on output

answer is 0.

Here's the code:


Expand|Select|Wrap|Line Numbers
  1.     #include<stdio.h>
  2.     #include<conio.h>
  3.     #include<stdlib.h>
  4.     #include"maska3.h"
  5.  
  6.     #define MAX_RES        3
  7.  
  8.     /*Functions prototypes*/
  9.  
  10.     float count(float r[],int type)
  11.     {
  12.         float re;
  13.  
  14.         switch(type)
  15.         {
  16.             case 1:re=r[0]+r[1]+r[2];
  17.                    break;
  18.             case 2:re=((r[0]*r[1]*r[2])/((r[2]*(r[0]+r[1])+r[0]*r[1])));
  19.                    break;
  20.             case 3:re=r[0]+((r[1]*r[2])/(r[1]+r[2]));
  21.                    break;
  22.             case 4:re=r[2]+((r[0]*r[1])/(r[0]+r[1]));
  23.                    break;
  24.             case 5:re=r[2]*((r[0]+r[1])/(r[0]+r[1]+r[2]));
  25.                    break;
  26.             case 6:re=r[0]*((r[1]+r[2])/(r[0]+r[1]+r[2]));
  27.                    break;
  28.             default: exit(1);
  29.         }
  30.  
  31.         return re;
  32.  
  33.     }
  34.  
  35.     void main(void)
  36.     {
  37.         int i,j,x,y,type;
  38.         float r[MAX_RES],re;
  39.  
  40.         textbackground(1);
  41.         textcolor(15);
  42.         clrscr();
  43.         okvir();/*This function creates a rectangle around screen.*/
  44.  
  45.         /* Initiating array.*/
  46.  
  47.         for(i=0;i<MAX_RES;i++) r[i]=0;
  48.         x=10;
  49.         y=5;
  50.         gotoxy(x,y);
  51.         cprintf("INPUT:");
  52.         gotoxy(x,y+1);
  53.         cprintf("様様様");
  54.         for(i=0;i<MAX_RES;i++)
  55.         {
  56.             y=y+2;
  57.             do{
  58.                 gotoxy(x,y);
  59.                 cprintf("R%d",i+1);
  60.                 gotoxy(x,y+1);
  61.                 cprintf("様");
  62.                 gotoxy(x+4,y);
  63.                 textbackground(3);
  64.                 for(j=0;j<=3*MAX_RES;j++) cprintf(" ");
  65.                 textbackground(1);
  66.                 gotoxy(x+6,y);
  67.                 scanf("%f",&r[i]);
  68.                 if(r[i]<=0)
  69.                 {
  70.                     textbackground(3);
  71.                     gotoxy(x+4,y);
  72.                     for(j=0;j<=3*MAX_RES;j++) cprintf(" ");
  73.                     textbackground(1);
  74.  
  75.                 }
  76.  
  77.             }while(r[i]<=0);
  78.  
  79.  
  80.         }
  81.         clrscr();
  82.         okvir();
  83.         gotoxy(x,y);
  84.         cprintf("OPTIONS:");
  85.         gotoxy(x,y+1);
  86.         cprintf("様様様様");
  87.         y=y+3;
  88.         gotoxy(x,y);
  89.         cprintf("1.All serial");
  90.         gotoxy(x,y+2);
  91.         cprintf("2.All parallel");
  92.         gotoxy(x,y+4);
  93.         cprintf("3.R1 serial connected to parallel R2 & R3");
  94.         gotoxy(x,y+4);
  95.         cprintf("4.R3 serial connected to parallel R1 & R2");
  96.         gotoxy(x,y+6);
  97.         cprintf("5.R3 parallel connected to serial connected R1 & R2");
  98.         gotoxy(x,y+8);
  99.         cprintf("6.R1 parallel connecterd to serila connected R2 & R3");
  100.         gotoxy(10,24);
  101.         cprintf("OPTION:");
  102.         type=getche();
  103.         getch();
  104.         clrscr();
  105.         re=count(r,type);
  106.         okvir();
  107.         gotoxy(10,10);
  108.         cprintf("Re:%.2f",re);
  109.         getch();
  110.  
  111.     }
Why is this happening?
Mar 12 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
you read type in as a character using getche() - I think your switch() should therefore use characters in the selection, e.g.
Expand|Select|Wrap|Line Numbers
  1.             case '1':re=r[0]+r[1]+r[2];
  2.                    break;
  3.             case '2':re=((r[0]*r[1]*r[2])/((r[2]*(r[0]+r[1])+r[0]*r[1])));
  4.                    break;
  5.  
Mar 12 '07 #2

100+
P: 1,646
Hi. If you stick with scanf() this should work. Replace this part:
Expand|Select|Wrap|Line Numbers
  1. type=getche();
  2. getch();
With this:
Expand|Select|Wrap|Line Numbers
  1. scanf("%d",&type);
Mar 12 '07 #3

Savage
Expert 100+
P: 1,764
you read type in as a character using getche() - I think your switch() should therefore use characters in the selection, e.g.
Expand|Select|Wrap|Line Numbers
  1.             case '1':re=r[0]+r[1]+r[2];
  2.                    break;
  3.             case '2':re=((r[0]*r[1]*r[2])/((r[2]*(r[0]+r[1])+r[0]*r[1])));
  4.                    break;
  5.  
I can't believe that I forgot those,I didn't even noticed.

Thanks for help.
Mar 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.