TRENDING NEWS

POPULAR NEWS

Help Me Correct My C Code

What's wrong with my C code? Can someone help me correct it? Thanks?

I really don't like the design. But it seems "specified" that way, so I can't argue with you and instead just have to accept it.

So I see that you pass three parameters and that the first two are said to be "int" but, in fact, you want them to be "float." So you might try that. But I also see that you really don't need them.

What you've done is kind of bizarre, using them for control, not data. Except that inside the function one of them is actual data. Which causes a real problem for you because the scanf() is expecting a float but it gets to only use an int there. If the physical size of the float is the same as the int (which if often is on 32-bit systems), then it "kind of works" (only in the sense that scanf() has no idea what it is stuffing and doesn't care, just so long as there are enough bytes to hold the result)... but... then in the next line which computes the new Req, the value MUST be interpreted as an int. But since scanf() stuffed a float organized value there, it will compute VERY STRANGE numbers for you.

You could fix that by just changing the int to a float in the parameters for resistorₑq(). At least, that would make things work. And you'd compute the right values, finally. But it is still pretty a hokey design. Even given the requirements, which are hokey enough already.

So here is something a little bit better.

#include
void resistor_eq( float Req ) {
    float resistor;
    for ( ; ; ) {
        printf("Enter the value of a resistor parallel to Req:");
        scanf("%f", &resistor);
        if ( resistor <= 0.0 ) break;
        Req= (Req*resistor)/(Req+resistor);
        printf("\tThe Equivalent Resistor is=%.2f\n",Req);
    }
}
int main( void ) {
    float Req;
    printf("Enter the value of the first resistor:");
    scanf("%f", &Req);
    printf("Req is equal to : %.2f\n", Req);
    resistor_eq(Req);
    return 0;
}

It still grinds my teeth to see it. But it's designed more in line with what you really should be doing, keeping most of your design structure in place. Those parameters really should be completely removed and they are, as shown.

It is because when you compile your code on the Code chef compiler, it checks the code for the sample input only which is given in the problem statement,while during final submission, it checks for a number of test cases including that sample case. So, I guess your code is working correctly for that sample case only and not for other test cases. I hope this helps.

Dude, stackoverflow is the worst site to correct your code. Your questions will be downvoted and if it continues you will be banned from questioning.What you are looking for is someone to point out bug in your code. So let me tell you one thing:One thing that programmer hate more than slow internet is reading others code that too poorly written to correct it.So here is what you should do.If error == syntax error then compiler or interpreter would be showing errors itself. Just look for syntax on internet and doneif error == logical error || semantics error || runtime error then read some other solution or learn how to use a debugger

Does anyone know how to correct this code in c++?

// EXERCISE 1 - Page 81
//------------------------------------...
//1. Write a program that prints out the entire lyrics to a full rendition of "99 bottles of beer"15

#include
using namespace std;

int total_beer;
int substract_beer;

int main(){


int total_beer = 99;
int substract_beer = total_beer - 1;

for (int total_beer = 99; total_beer <= 99; i--){
cout << total_beer " bottles of beer on the wall, " substract_beer " bottles of beer. ";
}

system ("PAUSE");
return 0;
}

For best answer, please correct and give full code.

Can Someone Please Post Correct Code for this C++ problem? I Need Help!?

Hi everyone, I need help with this problem for my C++ class. This comes from the Starting Out with C++ From Control Structures through Objects 7th edition. Here are the instructions:
Total Purchase
A customer in a store is purchasing five items. The prices of the five items are:

Price of item 1 = $12.95
Price of item 2 = $24.95
Price of item 3 = $6.95
Price of item 4 = $14.95
Price of item 5 = $3.95
Write a program that holds the prices of the five items in five variables. Display each item’s price, the subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 6%.

I did the code for this problem, but it did not work when I ran it on Microsoft Visual Studio Express 2012. Can anyone please post the correct code for this assignment in your answer so it could actually work for me when I run it? Thank you so much for your help. I will post my code down below just to show you what I have done:

#include
using namespace std;

int main()
{
float item1 = 12.95;
float item2 = 24.95;
float item3 = 6.95;
float item4 = 14.95;
float item5 = 3.95;
float tax = .06;
float saleSub = item1 + item2 + item3 + item4 + item5;
float saleTax = saleSub * tax;
float saleTotal = saleSub + saleTax;

cout << "Purchased Items:" << endl;
cout << "Item 1: $" << item1 << endl;
cout << "Item 2: $" << item2 << endl;
cout << "Item 3: $" << item3 << endl;
cout << "Item 4: $" << item4 << endl;
cout << "Item 5: $" << item5 << endl;
cout << "\nSale Subtotal: $" << saleSub << endl;
cout << "Sales Tax: $" << saleTax << " on current " << tax << " tax rate" << endl;
cout << "Total Sale Amount: $" << saleTotal << endl;
cout << "\n Thank you for shopping, come again!" << endl;

return 0;
}

How can I correct my cipher java program?

... deleted...

I deleted my answer because, upon testing, it had significant logic errors.

+add
I worked on this problem and this is a solution that's not very elegant but is simple.

char[] ltr = {'a', 'b', 'c', 'c', 'd', 'e','f','g','z'};
String str = "This is the alphabet: abcdefghij...z";
StringBuffer decode = new StringBuffer(str);
// String decode = new String(str);
char ch;
char repch;
for (int j = 0; j < decode.length(); j++) {
ch = repch = decode.charAt(j);
for (int k = 0; k < ltr.length; k++) {
if (ch == ltr[k]) {
if (k == 0) {
repch = ltr[ltr.length - 1];
} else {
repch = ltr[k - 1];
}
break;
}
}
decode.setCharAt(j, repch);
// System.out.println(str + " -> " + decode);
}
System.out.println(str + " -> " + decode);

** results:

This is the alphabet: abcdefghij...z -> This is thd zlphzadt: zabcdefhij...g

I didn't fill in the ltr array completely.
You can convert a StringBuffer object back to a string with:
String decodedStr = decode.toString();

Please don't delete your questions after getting answers: leave them so that others can benefit.

++add
That inner if to handle the special case of a->z bothered me. Here's a way to do it with modulo arithmetic
repch =
ltr[(k + ltr.length - 1) % ltr.length];

Also, I filled in the ltr array and decoded your message:
char[] ltr = {'a', 'b', 'c', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z'};
String str = "NFFU NF BU 23 JO UIF CFMM UPXFS";
str = str.toLowerCase();

**result:
nffu nf bu 23 jo uif cfmm upxfs -> meet me at 23 in the bell tower

Is my C code for finding the max value in an array correct?

#include

// function declaration
// Returns the index of max element
int max_elem_location(int[], int);

int main() {
int location; //you will need more variables
int size;

printf("Input number of elements in array\n");
scanf("%d", &size);
int array[size];

printf("Enter %d integers\n", size);
for (int i = 0; i < size; i++)
{
scanf("%d\n", &array[i]);
}

location = max_elem_location(array, size);

Your first problem is here:cin >> input[MAX]; You're putting your input right off the end of the array. Arrays are zero-indexed, so your statement (using the MAX constant) is equivalent to this:cin >> input [25];That array (input) has 25 members, with indexes 0-24. You're not putting the value in your array at all.When you later iterate through your array, you're getting whatever random garbage was in memory where that array is, because you've never initialized its values. You instead need to use a counter that increments as the user puts in each value until they either enter -1 or fill the array.

Can you help me compile this c++ source code please?

here's the download link for the source code. its a program called "ecsedtrend"

http://iamg.org/documents/oldftp/VOL34/v34-07-08.zip

i tried compiling it myself in visual studio express 2013 but i don't know how... so
can you teach me how to compile it using visual studio express 2013?

i'm new to this programming stuff so i'm not even sure if visual studio express 2013 is the right tool for the job...

help me make this source code into an exe please!

TRENDING NEWS