Monday, May 24, 2010

Which of the following string declarations contains an error?

a. char String1[3] = "xyz";


b. char String2[5] = "abcd";


c. char String4[ ] = "world"

Which of the following string declarations contains an error?
char String1[3]="xyz" is wrong. The correct declaration is char String1[4]="xyz"





Nelson Bittencourt


See my site:


http://www1.webng.com/nbittencourt/index...


Can someone pleaseeee tell me whats wrong with my code am stuckk....?

it supposed to calculate the amount due for the vehicle parked in a garage


the user enters type of car,hour arrival,minute arrival,exit hour,exit arrival then it supposed print it out in military time and charge and am not getting that any advices pleaseee thank youuu








import java.util.Scanner;


public class Assignment


{


public static void main(String [] args)


{


double hour = 0.0;


double minute = 0.0;


double cost = 0.0;


char C,c;


char V,v;





Scanner keyboard=new Scanner(System.in);


System.out.println("What kind of car do you have? C for car || V for van");





System.out.println("Enter arrival hour 0-23 ");


hour=keyboard.nextInt();





System.out.println("Enter arrival minute 0-59 ");


minute=keyboard.nextInt();





System.out.println("Enter exit hour ");


minute=keyboard.nextDouble();





System.out.println("Enter exit minute ");


minute=keyboard.nextDouble();





System.out.println("**************");


System.out.println("XXXXX");


System.out.println("**************");

Can someone pleaseeee tell me whats wrong with my code am stuckk....?
If I understand what you want properly, here is a program to complete what I think you want. Add more info if you need more help or if I misunderstood you.





// START OF CODE


import java.util.Scanner;


public class carvan


{


public static void main(String [] args)


{


// resulting values


double cost = 0.0;


boolean Van = false;


boolean Car = false;





// input data


double arrivalHours = 0.0;


double arrivalMinutes = 0.0;


double exitHours = 0.0;


double exitMinutes = 0.0;


String type; //type of vehicle





Scanner keyboard=new Scanner(System.in);


System.out.println("What kind of car do you have? C for car || V for van ");


type = keyboard.next();


if(type.charAt(0)=='C' || type.charAt(0)=='c')


Car = true;


else if(type.charAt(0)=='V' || type.charAt(0)=='v')


Van = true;





System.out.println("Enter arrival hour 0-23 ");


arrivalHours=keyboard.nextInt();





System.out.println("Enter arrival minute 0-59 ");


arrivalMinutes=keyboard.nextInt();





System.out.println("Enter exit hour ");


exitHours=keyboard.nextDouble();





System.out.println("Enter exit minute ");


exitMinutes=keyboard.nextDouble();





System.out.println("**************")...


System.out.println("XXXXX");


System.out.println("**************")...





double totalMins = ((exitHours*60.0)+exitMinutes) - ((arrivalHours*60.0)+arrivalMinutes); // find time stayed for





if( Van ) // for a van


{


if( totalMins %26gt;= 60.0*8.57 ) // if it is more than or = to 8.57 hours, the cost will be $30


cost = 30.0;


else // if parked for less than 8.57 hours


cost = 3.50 * Math.ceil(totalMins/60.0); // 3.50 per hour (and round hours up, ex 3:20 = 4 hours)


}


else if( Car ) // for a car


{


if( totalMins %26gt;= 60.0*10.0 ) // if parked for more than or = to 10 hours, the cost will be $20


cost = 20.0;


else // if parked for less than 10 hours


cost = 2.0 * Math.ceil(totalMins/60.0); // 2.00 per hour


}





System.out.printf("Parking a " + (Van?"van":"car") + " for %02d:%02d will cost $%.2f.", (int)Math.floor(totalMins/60), ((int)totalMins)%60, cost);


}


}


// END OF CODE


Please ask here if you need more help! ;P
Reply:Hello,


There is a lot wrong with this program, first the syntax is wrong. Then I am not sure the scope of your program but it should check for invalid data. Like what if I enter 53 for the hour? First get it to compile. Then you can test output and stuff.





If you can tell me exactly what you have to do, I can help you.
Reply:ahah
Reply:First, I don't think you fully grasp the concept of a char. Just as an int holds an integer and a double holds a decimal number, the char type holds characters. So, when declaring your instance variables you have written "char C, c;" and "char V, v;". Those two lines simply create variables to hold letters. Just like saying "double cost" creates a variable called "cost" that holds doubles, saying "char C" simply creates a variable called "C" that holds a character.





What this means, is that when you ask what kind of car the person has, you still need to use your Scanner to take in that char. Them simply typing a letter means nothing to your code.





Also, you made 4 char variables: C, c, v and V. I'm guessing you did this to account for case, that is, if the user types in C or c it will work. It was a noble attempt, but once again, chars don't work that way. So, what you should do is create a char variable called "car" or "vehicle", and create only one.





So replace your two lines that create char's with simply: char vehicle;.





Now, I noticed that when you ask the user what type of vehicle they have you use the "or" operator(||). Now, you could do this, but it's not necessary and most users won't know what that means. Remember, when you enclose something in double quotes ("") that's a string literal, so the computer won't read it, it will simply print it out. So a better question to ask your user is simply: "What kind of car do you have?(C for car or V for van)"





The next line of your code needs to assign the user input to your vehicle variable. So your next line of code should be this: vehicle = keyboard.nextChar();





Now the vehicle variable is holding the user input. So for now, we're good here.





Your next problem is that you assign multiple values to one variable, so only the last one shows.





Notice after you ask for arrival time, exit hour, and exit minute you assign the user input all to one variable. This won't work, because if you assign a value to a variable, it overwrites whatever was already there.





For example, let's say the user arrival minute was 27. They input 27 when prompted and 27 is assigned to the variable minute. Then when prompted they input 2 for exit hour. You have also assigned this value to the minute variable, which means that the 27 that was previously there, is overwritten! So you've lost that value. Then you do it again when asking for exit minute. The 2 that was stored in minute, is once again overwritten by the user input.





Don't worry though, this is a simple fix. Simply create 4 separate variables for arrival hour, arrival minute, exit hour, and exit minute.





So, replace "double hour" and "double minute" with this line of code: int arrivalHour, arrivalMinute, exitHour, exitMinute;





That line creates 4 int variables called arrivalHour, arrivalMinute, exitHour, and exitMinute.





Also, because these are int values, you must change your scanner to accept ints. So where you have written "keyboard.nextDouble();" you must write "keyboard.nextInt();".





Now you have 4 separate variables for arrival and exit time, and one char variable that represents car or van. We're doing well, but there's still more to be done with the rest of your code.





In this line: "double totalTime = (hour * 60.0) + minute;" you attempt to calculate the total number of minutes the user has been in the garage. Well, you have the right idea, but you aren't quite there.





How I would do this, is subtract the number of minutes past midnight that the user got there, from the number of minutes past midnight that the user left.





For instance, if the user arrived at 12:30 AM, and left at 1:45 AM, then the user arrived 30 minutes after midnight, and left 105 minutes after midnight. Therefore 105 - 30 would give you the number of minutes the user has been there.





This is easily done(oh and I'll be using int's instead of doubles since we're dealing with int's in the rest of your code: "int totalTime = (exitHour * 60 + exitMinute) - (arrivalHour * 60 + arrivalMinute);"





This code leaves us with the number of minutes the user has been there in the totalTime variable.





(We're not quite done here but I"m gonna post this to get you started and come back and finish it later)





edit:





Alright, almost done. You're next step is to check whether the vehicle is a car or a van. You had the right idea with your if statements, but you can't simply say if(van) without having a boolean variable called van.





So, a better way to do it is:


"if(vehicle == 'v' || vehicle == 'V') {


//do code for van


}else{


//do code for car


}"





In the actual if statement you're determining whether the vehicle variable holds a V or a v. Remember to enclose in single quotes('') because those are used for chars. You happen to know that if the vehicle is not a van, then it's a car. So you don't need another if statement asking if it's car, you know it is if it's not a van (so you use an else).





For the van code you need to check if the minutes are greater than 514 to assign a flat rate, or if less than 514 assign an hourly rate.





The first problem here is that instead of "if(hour %26gt;= 514)" you should write "if(totalTime %26gt;= 514)"





Then, instead of writing the opposite of that (if it's less than), you happen to know that if it's not greater than 514, it's less than. So use an else rather than a second if. And if it is less than 514, you need to calculate the number of hours the user has been there. Do this simply by writing: "cost = 3.5 * (exitHour - arrivalHour);"





For your car code, it's the same deal. Use the same adjustments you did for van in car.





Nearly done now.





Next, in your first S.o.p you need to say "vehicle" instead of "car".





In your second S.o.p you can simply say ("Exit time: " + exitHour + ":" + exitMinute);





You can then eliminate your third S.o.p.





And that's all! I'll write up the revised code for you and post it. And if you have any other questions feel free to send me an email.








edit:





Here's the revised code:





import java.util.Scanner;





public class Assignment


{


public static void main(String [] args)


{


int arrivalHour, arrivalMinute, exitHour, exitMinute;


double cost = 0.0;


char vehicle;





Scanner keyboard=new Scanner(System.in);





System.out.println("What kind of car do you have?(C for car or V for van)");


vehicle = myScanner.nextChar();





System.out.println("Enter arrival hour 0-23 ");


arrivalHour = keyboard.nextInt();





System.out.println("Enter arrival minute 0-59 ");


arrivalMinute = keyboard.nextInt();





System.out.println("Enter exit hour ");


exitHour = keyboard.nextInt();





System.out.println("Enter exit minute ");


exitMinute = keyboard.nextInt();





System.out.println("**************");


System.out.println("XXXXX");


System.out.println("**************");








int totalTime = (exitHour * 60 + exitMinute) - (arrivalHour * 60 + arrivalMinute);





if(vehicle == 'v' || vehicle == 'V') {


if(totalTime %26gt;= 514) { // more than 8.57 hours


cost = 30.0;


}else{


cost = 3.5 * (exitHour - arrivalHour); // 3.50 per hour


}





}else{


if(totalTime %26gt;= 600.0 ) { // more than 10 hours


cost = 20.0;


}else{


cost = 2 * (exitHour - arrivalHour); // 2.00 per hour


}


}





System.out.println("Parking for a" + vehicle);


System.out.println("Exit time: " + exitHour + ":" + exitMinute);


System.out.println("You need to pay : " + cost);


}


}


Which of the following string declarations contains an error?

a. char String1 [3]="xyz"


b. char String2 [5]="abcd"


c. char String 3 [7]="hello"


d. char String4 [ ] = "world"

Which of the following string declarations contains an error?
C. Variable names must be consecutive names with no spaces.

free survey

Please help me in this password code ?

Hi


I am inputing same password as akku . why its showing "wrong". Please tell me the mistake and giuide me in this code#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





main()


{


char c;


char ppp[20] = "akku"; // correct password


char password[20];


int cnt=0;





printf("password:");





while( (c=getch()) != 13 ) // 13 is the code for enter


{


password[cnt] = c;


printf("*");


cnt++;


}


password[cnt] = '\0';





if(password==ppp)


printf("correct");


else


printf("wrong");





getch();


}

Please help me in this password code ?
Change if(password==ppp) to if(!strcmp(password,ppp)) .


Characters (strings) in C/C++ can not be compared with == operator . You have to use strcmp function instead .





Don't forget to include string.h header file .
Reply:An elaboration on the last answer:


When you create ppp[20] and password[20], you request the compiler to create these two lines:


char *ppp, *password;


ppp=new char[20];


password=new char[20];


This causes the name of the array to actually be a pointer to its first element, meaning the expansion of the expression:


a[x]


is actually:


*(a+x)


which works because of pointer arithmetic.


When you do the equality, it can be guaranteed that ppp and password do NOT have the same address, which happens to be what you are comparing. The only work around is to compare size then loop for every character. Luckily, strcmp(const char *, const char *) %26lt;string.h%26gt; does this for you, and returns 0 for equal strings, -1 for "less than", and 1 for "greater than".


A few simple questions from a newbie. About certain code.?

Hello. I used to program in C# a little bit, but after reading some things I've decided to push forward on C++. I have some doubts though that I can't seem to find the answer anywhere.





#include "Hello World.h"





int main (int argc, char* argv[])


{


//std::cout%26lt;%26lt;"Hello, World!";


std::cout%26lt;%26lt;"Oh my lord. This is me toying with the programmming code C++";


char myLine[100];


std::cin.getline(myLine,100);


return 0;


}





1. Do I have to always include? [code]int main (int argc, char* argv[])[/code]





2. I want to print "Hello World!" and on a separate line "Oh my lord. This is me toying with the programming code C++"





3. What does std, cout, cin mean? Are they short for something else? I'd like to know that way I'll remember them easily.





Thanks for the help guys. :)

A few simple questions from a newbie. About certain code.?
1. Sort of, you need a main function, but it can look like: int main() only.





2. You want to print out an end of line which is the string std::endl, so std::cout %26lt;%26lt; std::endl; in between your lines should do it.





3. std is the standard namespace, cout means C output and C input for cin.
Reply:std just means to use the standard cout, not any other cout that might be lying around.





If you put this line after your includes





using namespace std;





then you can get rid of the std::





Then for doing a newline you should use endl;





cout %26lt;%26lt; "Hello, World!" %26lt;%26lt; endl;





All programs need a main.





cout is the standard character output stream


cin is standard character input stream
Reply:I don't know what you read that made go from C# to C++ but I'll try to answer your questions:





Unless you are righting a very specific and simple program using the VOID MAIN(){....} //uses no return statement


And perhaps some simple functions added to that, then you do not need to include any header files.





I can guarantee though that there is no header file to include called "Hello World.h" unless you created one for some strange reason with code in it.





you do not require int main(int argc, char * argv[]) //unless you need your program to accept arguments at the command line (in DOS this would be something like "C:\%26gt;MyProgram.exe ARGS" or in UNIX, "Guest@UnixServer%26gt;./MyExecutable ARGS"





cout is a function, in which you need to include iostream: #include%26lt;iostream%26gt;, cout means console-out and will display what ever you need to, to the console (the screen).





cin also comes from the iostream header file and means take console input (keyboard) and store in a variable: cin %26gt;%26gt; myString;





I am not too familiar with std:: and I believe it has something to do with telling the code to reference the standard library (the header files). I don't think it is necessary if you include the correct header(s) depending on what you need (iostream, string, math, vector, etc). The only time you would need it is if you created something else that used cout or cin which would not be too smart.





I would highly suggest you stick with C#. C++ is great and it is where I did most of my development in college and a little before, but C# is perfect for Windows programming or building Web applications.





Depending on the compiler of C++ you are using, syntax may vary.





Unless you have a specific need to stay with C++, you should do C#. They are similar in syntax but it is easier in C# to create console apps, standalone apps, and web apps. You can even get express editions for free of C#, VB, and VisualC++ from microsoft.
Reply:You dont have to include the file if the file is incorparted. I dont know whats inside the hello world.h headers though.





Mind you The onyl knowledge I have for C++ is the general knowledge since I am a vb.net guy.





I think the cout means Line Out. Pretty much like "print on the screen".





Then the char myLine[100]; declares a varible. I dont know what the hundred means, but I have a 99% hunch that its a buffer in bytes.





the cin means "get from the screen" and the two parameters you used mean that the input from the from the cin should be stored in the variable.





the std probably just means that its getting the functions from the string class.





then you just return zero as your not returning anything in the function.





I hope that helps. I dont know anything about C++. I would never do C++ unless I am a professional at it. Its just .. its such a STRONG and LOW LEVEL language that even a simple print, and get statements require tedious code.





heheh


Read chars untill press enter. if press enter show d ans hw manytimes vowels r occured.code must be 1line in c

I will try:


int d;


scanf,("%c",%26amp;charR);


if (strcmp(charR, "r") == 0)


{ d++; }


printf ("R appears %s ",d," times\n");


What do you think about Law Enforcement in Onslow N.C?

Urgency of Search for Marine Questioned


By MIKE BAKER – 3 hours ago





JACKSONVILLE, N.C. — For months after a pregnant 20-year-old Marine accused a colleague of rape, her family says, she continued to work alongside her attacker and endured harassment at Camp Lejeune. In the weeks after she disappeared, they believe, the sheriff's department was slow to act.





As authorities recovered Maria Lauterbach's remains Saturday from a fire pit where they suspect Marine Cpl. Cesar Armando Laurean burned and buried her body, her family asked why authorities didn't treat her case with greater urgency.





Naval investigators on Saturday said the pair had been separated on the job, a rape case was progressing and Laurean was under a protective order to stay away from Lauterbach. And Onslow County Sheriff Ed Brown insisted his department acted as best they could on the facts available.





"As soon as it went suspicious, we contacted the media and asked for help," Brown said. "The case did not produce enough evidence, other than she was just missing."





On Saturday, her burnt remains, and those of her unborn child, were excavated from Laurean's backyard.





"As well as I could see, the body was much charred," Brown said. "The fetus was in the abdominal area of that adult. ... That is tragic, and it's disgusting."





Authorities have issued an arrest warrant on murder charges for Laurean, 21, of the Las Vegas area. They say he fled Jacksonville after leaving behind a note in which he admitted burying her body.





In his note, Laurean wrote Lauterbach cut her own throat in a suicide, but Brown doesn't believe it and challenged Laurean to come forward and defend his claims of innocence.





Authorities have described a violent confrontation inside Laurean's home that left blood spatters on the ceiling and a massive amount of blood on the wall.





County prosecutor Dewey Hudson said Laurean had been in contact with three attorneys, including Mark E. Raynor, who declined to comment Saturday.





Lauterbach disappeared sometime after Dec. 14, not long after she met with military prosecutors to talk about her April allegation that Laurean raped her.





Her uncle, Pete Steiner, said that Lauterbach — stung by the harassment that eventually forced her to move off base — decided to drop the case the week before she disappeared.





Paul Chiccarelli, the special agent in charge of Naval Criminal Investigative Service at Camp Lejeune, told The Associated Press on Saturday that Marine commanders submitted requests in October to send the case to the military's version of a grand jury. A military protective order had been automatically issued in May and renewed three times.





"Anytime there is a sexual assault allegation involved, that's a standard routine," he said.





Lauterbach and Laurean served in the same unit of the II Marine Expeditionary Force, and court documents indicate Lauterbach's mother told authorities Laurean had threatened her daughter's career.





Steiner said Saturday on ABC's Good Morning America the Marines didn't separate the two personnel clerks, but Chiccarelli said Marine commanders assigned them to separate buildings on May 12.





Neither Brown or Hudson would say Saturday if they would have treated the case differently had they known about the protective order, which they discovered Friday night.





Chiccarelli said sheriff's office investigators were told about the order on Monday.





But Chiccarelli again said investigators didn't consider Laurean a threat to Lauterbach, or later a flight risk, because they had indications the pair were on friendly terms. He declined to detail those indications on Saturday.





Lauterbach's mother reported her daughter missing Dec. 19 — five days after she last spoke with her. By that time, she had been placed on "unauthorized absence" status by the Marine Corps.





"Several steps were taken to contact her via telephone, cell phone, even in person by sending Marines to her residence," said II MEF spokesman Lt. Col. Curtis Hill. "At that time, there was no reason to believe anything other than she had voluntarily placed herself in an unauthorized absence status."





Hill said that Lauterbach also left her roommate a note saying she was "going away" and apologized for "the inconvenience."





An Onslow County Sheriff's employee contacted Naval investigators Dec. 19 after hearing from police in Ohio and listed her as a "missing person at risk" in a national law enforcement database. He met with Lauterbach's roommate the next day, but court documents indicate he was unable to reach the Marine officer who had been notified of her absence, as he was away on holiday leave.





The employee checked ditches along several highways for her car, and asked the State Highway Patrol and several area hospitals if they had had any contact with the missing Marine. None had. He left word with the department radio room to contact him with any developments before leaving Dec. 22 for a vacation.





Steiner said he and his sister, Maria's mother, told authorities they planned to fly to North Carolina around the Christmas holiday, but were advised not to because authorities believed Lauterbach was headed for Dayton.





Believing that authorities "dropped the ball," Steiner said the Lauterbachs eventually decided they could no longer wait. They flew to North Carolina and met with detectives Monday, the same day court documents indicate authorities first discovered Lauterbach's ATM card had been used by a white male on Christmas Eve and she missed a prenatal care appointment on Dec. 26.





Brown also learned about the case Monday. A series of search warrants were filed, and the case went public and he asked for help.





By that point, it was too late. Laurean refused to meet with investigators, and eventually skipped town before dawn Friday without telling his lawyers where he was going.





Associated Press writers Estes Thompson in Raleigh and Jim Hannah in Vandalia, Ohio, contributed to this report

What do you think about Law Enforcement in Onslow N.C?
I could tell you , I used to live there!Right off Navy Blvd ,down from the front gate.


It's a small country town,that is a town because of the base.


Maybe that's one reason their slow,just my opinion.


The base wants to protect their image and try to keep this under wrap -not leak bad press of the Corps to the press. It just make all the Marines look bad.


It is disgusting and the man will get what's coming to him.

survey results