Code: Select all
2
xxxxxxx xxxx xxxx I=0.0000000000000001MA yyy yyy U=1000000000000000.000000000000mV zzz
Can you prove P>1mW for I=1A and U=1V?
Moderator: Board moderators
Code: Select all
2
xxxxxxx xxxx xxxx I=0.0000000000000001MA yyy yyy U=1000000000000000.000000000000mV zzz
Can you prove P>1mW for I=1A and U=1V?
Code: Select all
if(fi==1&&fu==1)
{
printf("Problem #%d\n",count);
printf("P=%.2LfW\n\n",i*u);
}
if(fi==1&&fp==1)
{
printf("Problem #%d\n",count);
printf("U=%.2LfV\n\n",p/i);
}
if(fu==1&&fp==1)
{
printf("Problem #%d\n",count);
printf("I=%.2LfA\n\n",p/u);
}
Code: Select all
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void readVal(int); //reads converts, and stores numbers into p,u, or i.
void calculate(); //calculates the missing variable and outputs to screen
float p, u, i;
char used[2]; //2 characters to indicate the variables already obtained
int counter; //keep track of how many variables obtained.
string line; //holds the contents of the entire line
int main(){
int cases, k, j;
cin>>cases;
getline(cin, line);
for (k=0; k<cases; k++){
cout<<"Problem #"<<k+1<<endl;
getline(cin, line);
j=0;
counter = 0;
p=0;
i=0;
u=0;
//This takes into account if the first character in the line is a variable P,I, or U
if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
if (line[j+1] == '='){
readVal(j);
j=j+3;
}
}
while (counter< 2) {
if (line[j] == ' '){
j++;
if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
if (line[j+1] == '='){
readVal(j);
j=j+3;
}
}
}
else
j++;
}//end while
calculate();
}//end for
return 0;
}
void readVal(int k){
char c[20];
float num;
int j; //used to index char c[]
if (counter == 0){
used [0] = line[k];
}
else{
used [1] = line[k];
}
counter++;
k=k+2;
j= 0;
c[j] = line[k];
while (c[j] != 'M' && c[j] != 'm' && c[j] != 'k' && c[j] != 'W' && c[j] != 'V' && c[j] != 'A'){
j++;
k++;
c[j] = line[k];
}//end while
//takes into account the prefixes, if any
num = atof(c);
if (c[j] == 'M')
num = num * 1000000;
else if (c[j] == 'm')
num = num / 1000;
else if (c[j] == 'k')
num = num * 1000;
if (counter == 1){
if (used[0] == 'P')
p = num;
else if(used[0] == 'I')
i= num;
else
u=num;
}
else {
if (used[1] == 'P')
p = num;
else if(used[1] == 'I')
i= num;
else
u=num;
}
}//end readVal;
void calculate(){
if (used[0] !='P' && used[1] != 'P'){
p=i*u;
cout<<"P="<<fixed<<setprecision(2)<<p<<"W"<<endl<<endl;
}
else if (used[0] !='I' && used[1] != 'I'){
i=p/u;
cout<<"I="<<fixed<<setprecision(2)<<i<<"A"<<endl<<endl;
}
else{
u=p/i;
cout<<"U="<<fixed<<setprecision(2)<<u<<"V"<<endl<<endl;
}
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
void readVal(int); //reads converts, and stores numbers into p,u, or i.
void calculate(); //calculates the missing variable and outputs to screen
float p, u, i;
char used[2]; //2 characters to indicate the variables already obtained
int counter; //keep track of how many variables obtained.
char line[200]; //holds the contents of the entire line
int main(){
int cases, k, j;
fgets(line, 200, stdin);
cases = atoi (line);
for (k=0; k<cases; k++){
printf("%s%d\n", "Problem #" ,k+1);
bzero(line, sizeof(line));
fgets(line, 200, stdin);
j=0;
counter = 0;
p=0;
i=0;
u=0;
//This takes into account if the first character in the line is a variable P,I, or U
if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
if (line[j+1] == '='){
readVal(j);
j=j+3;
}
}
while (counter< 2) {
if (line[j] == ' '){
j++;
if (line[j]=='P' || line[j]=='I' || line[j]=='U'){
if (line[j+1] == '='){
readVal(j);
j=j+3;
}
}
}
else
j++;
}//end while
calculate();
}//end for
return 0;
}
void readVal(int k){
char c[20];
float num;
int j;
if (counter == 0){
used [0] = line[k];
}
else{
used [1] = line[k];
}
counter++;
k=k+2;
j= 0;
c[j] = line[k];
while (c[j] != 'M' && c[j] != 'm' && c[j] != 'k' && c[j] != 'W' && c[j] != 'V' && c[j] != 'A'){
j++;
k++;
c[j] = line[k];
}
num = atof(c);
if (c[j] == 'M')
num = num * 1000000;
else if (c[j] == 'm')
num = num / 1000;
else if (c[j] == 'k')
num = num * 1000;
if (counter == 1){
if (used[0] == 'P')
p = num;
else if(used[0] == 'I')
i= num;
else
u=num;
}
else {
if (used[1] == 'P')
p = num;
else if(used[1] == 'I')
i= num;
else
u=num;
}
}
void calculate(){
if (used[0] !='P' && used[1] != 'P'){
p=i*u;
printf("%s%.2f%s\n\n", "P=",p,"W");
}
else if (used[0] !='I' && used[1] != 'I'){
i=p/u;
printf("%s%.2f%s\n\n", "I=",i,"A");
}
else{
u=p/i;
printf("%s%.2f%s\n\n", "U=",u,"V");
}
}
Hey Dominik,Dominik Michniewski wrote: [snip]
2. you don't set all options of compiler (I heard about something like gcc -wall -strict -ansi file.c to compile - check options of your compiler).
With my g++ 4.0.0 the above C++ did not compile. There were 2 errors:jjtse wrote: [snip][snipped]Code: Select all
#include <iostream> #include <stdlib.h> #include <iomanip> using namespace std; void readVal(int); //reads converts, and stores numbers into p,u, or i. void calculate(); //calculates the missing variable and outputs to screen float p, u, i; char used[2]; //2 characters to indicate the variables already obtained int counter; //keep track of how many variables obtained. string line; //holds the contents of the entire line int main(){ int cases, k, j; cin>>cases; getline(cin, line); for (k=0; k<cases; k++){ cout<<"Problem #"<<k+1<<endl; getline(cin, line); j=0; counter = 0; p=0; i=0; u=0; //This takes into account if the first character in the line is a variable P,I, or U if (line[j]=='P' || line[j]=='I' || line[j]=='U'){ if (line[j+1] == '='){ readVal(j); j=j+3; } } while (counter< 2) { if (line[j] == ' '){ j++; if (line[j]=='P' || line[j]=='I' || line[j]=='U'){ if (line[j+1] == '='){ readVal(j); j=j+3; } } } else j++; }//end while calculate(); }//end for return 0; } void readVal(int k){ char c[20]; float num; int j; //used to index char c[] if (counter == 0){ used [0] = line[k]; } else{ used [1] = line[k]; } counter++; k=k+2; j= 0; c[j] = line[k]; while (c[j] != 'M' && c[j] != 'm' && c[j] != 'k' && c[j] != 'W' && c[j] != 'V' && c[j] != 'A'){ j++; k++; c[j] = line[k]; }//end while //takes into account the prefixes, if any num = atof(c); if (c[j] == 'M') num = num * 1000000; else if (c[j] == 'm') num = num / 1000; else if (c[j] == 'k') num = num * 1000; if (counter == 1){ if (used[0] == 'P') p = num; else if(used[0] == 'I') i= num; else u=num; } else { if (used[1] == 'P') p = num; else if(used[1] == 'I') i= num; else u=num; } }//end readVal; void calculate(){ if (used[0] !='P' && used[1] != 'P'){ p=i*u; cout<<"P="<<fixed<<setprecision(2)<<p<<"W"<<endl<<endl; } else if (used[0] !='I' && used[1] != 'I'){ i=p/u; cout<<"I="<<fixed<<setprecision(2)<<i<<"A"<<endl<<endl; } else{ u=p/i; cout<<"U="<<fixed<<setprecision(2)<<u<<"V"<<endl<<endl; } }
Code: Select all
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int segments, cases, numLines;
float line[100][3];
float temp;
float axis[200];
int i,j,k;
int counter;
float mid, trans;
cin>>cases;
for (i=0; i<cases; i++){
cin>>numLines;
for(j=0; j<numLines; j++){
cin>>line[j][0];
cin>>line[j][1];
cin>>line[j][1];
cin>>line[j][2];
cin>>line[j][2];
if (line[j][1] < line[j][0]){
temp = line[j][1];
line[j][1] = line[j][0];
line[j][0] = temp;
}
axis[j*2] = line[j][0];
axis[j*2+1] = line[j][1];
}
//bubble sort
for (j=0; j<2*numLines; j++){
for (k=j+1; k<2*numLines; k++){
if (axis[j] > axis[k]){
temp = axis[j];
axis[j] = axis[k];
axis[k] = temp;
}
}
}
segments = numLines*2+1;
counter = 0;
cout <<segments<<endl;
for(j=0; j<segments; j++){
if (j==0){
cout<<"-inf"<<" "<<setprecision(3)<<fixed<<axis[counter]<<" "<<"1.000"<<endl;
counter ++;
}
else if (j == segments -1){
cout <<axis[counter-1]<<" +inf 1.000"<<endl;
}
else{
cout<<axis[counter-1]<<" "<<axis[counter]<<" ";
trans = 1.000;
mid = axis[counter] - axis[counter-1];
mid = mid/2;
mid = mid + axis[counter -1];
for (k=0; k<numLines; k++){
if (mid > line[k][0] && mid < line[k][1]){
trans = trans * line[k][2];
}
}
counter ++;
cout<<trans<<endl;
}
}
cout<<endl;
}//end for cases
return 0;
}
Code: Select all
Here are the compiler error messages:
03865847_24.c: In function `int main()':
03865847_24.c:53: `fixed' undeclared (first use this function)
03865847_24.c:53: (Each undeclared identifier is reported only once
03865847_24.c:53: for each function it appears in.)