I have an assignment for one of my classes where I need to sort a string alphabetically without using any commands besides the simple ones that are already in here. Whenever I use it, it works for the most part except it will leave words like Fred & Eric, or Hazel & Ian (first letter's are next to each other in the alphabet). The string that is being compared to the others is set as "two" then all others are compared against it. The B string is just one that is being changed with the A string. If anyone knows why this is, that would be greatly appreciated!
for (int ct = 0; ct < kh; ct++){
hold = A[ct];
bool pass = false;
for (int ct2 = ct+1; ct2 < kh; ct2++){
two = A[ct2];
if (two[0] < hold[0]){
save = A[ct2];
A[ct2] = A[ct];
A[ct] = save;
hold = two;
save = B[ct2];
B[ct2] = B[ct];
B[ct] = save;
}
else if (two[0] == hold[0]){
if (two[1] < hold [1]){
save = A[ct2];
A[ct2] = A[ct];
A[ct] = save;
hold = two;
save = B[ct2];
B[ct2] = B[ct];
B[ct] = save;
}
}
else if (two[1] == hold[1]){
if (two[2] < hold [2]){
save = A[ct2];
A[ct2] = A[ct];
A[ct] = save;
hold = two;
save = B[ct2];
B[ct2] = B[ct];
B[ct] = save;
}
}
}
}
Related
Basically I have in my program -
team[0].game[2] = loadvar[1];
team[0].game[3] = loadvar[2];
team[0].game[4] = loadvar[3];
team[0].game[5] = loadvar[4];
team[0].game[6] = loadvar[5];
team[0].game[7] = loadvar[6];
team[0].game[8] = loadvar[7];
team[0].game[9] = loadvar[8];
team[0].game[10] = loadvar[9];
team[0].game[11] = loadvar[10];
team[0].game[12] = loadvar[11];
team[1].game[1] = loadvar[13];
team[1].game[2] = loadvar[14];
team[1].game[3] = loadvar[15];
team[1].game[4] = loadvar[16];
team[1].game[5] = loadvar[17];
team[1].game[6] = loadvar[18];
team[1].game[7] = loadvar[19];
team[1].game[8] = loadvar[20];
team[1].game[9] = loadvar[21];
team[1].game[10] = loadvar[22];
team[1].game[11] = loadvar[23];
team[1].game[12] = loadvar[24];
team[2].game[1] = loadvar[26];
team[2].game[2] = loadvar[27];
team[2].game[3] = loadvar[28];
team[2].game[4] = loadvar[29];
team[2].game[5] = loadvar[30];
team[2].game[6] = loadvar[31];
team[2].game[7] = loadvar[32];
team[2].game[8] = loadvar[33];
team[2].game[9] = loadvar[34];
team[2].game[10] = loadvar[35];
team[2].game[11] = loadvar[36];
team[2].game[12] = loadvar[37];
team[3].game[1] = loadvar[39];
team[3].game[2] = loadvar[40];
team[3].game[3] = loadvar[41];
team[3].game[4] = loadvar[42];
team[3].game[5] = loadvar[43];
team[3].game[6] = loadvar[44];
team[3].game[7] = loadvar[45];
team[3].game[8] = loadvar[46];
team[3].game[9] = loadvar[47];
team[3].game[10] = loadvar[48];
team[3].game[11] = loadvar[49];
team[3].game[12] = loadvar[50];
team[4].game[1] = loadvar[52];
team[4].game[2] = loadvar[53];
team[4].game[3] = loadvar[54];
team[4].game[4] = loadvar[55];
team[4].game[5] = loadvar[56];
team[4].game[6] = loadvar[57];
team[4].game[7] = loadvar[58];
team[4].game[8] = loadvar[59];
team[4].game[9] = loadvar[60];
team[4].game[10] = loadvar[61];
team[4].game[11] = loadvar[62];
team[4].game[12] = loadvar[63];
I'm retrieving the data from a text file.
Basically, in the program, I'm having to copy and paste the same thing over and over again, and increasing the array number for the team and loadvar. Is there anyway I can copy and paste it, and it do the number increasing for me?
Simply don't copy + paste, rather use loops to do the incrementing numbers, e.g. for
int v = 1;
for (int t = 0; t < 5; ++t)
{
for (int g = 1; g <= 12; ++g)
{
team[t].game[g] = loadvar[v++];
}
}
I've kept game indexed from 1-12 as in your question, but I suspect you might mean to index from 0-11. Be careful of this common source of bug for beginner programming.
If you really want to copy and paste, you can write something like this
int i = 0;
int j = 2;
int k = 1;
team[i].game[j++] = loadvar[k++];
team[i].game[j++] = loadvar[k++];
and so on. However the shorter and less error prone way would be two nested for loops.
Currently I am error trapping in my program that if my webscraper is sent a link that shows "Quote Not Found Page For WSJ Market Data" in the source of the page that it will set all my variables to 0, but when I run my code even if i type in a website that I know should work it sets all my variables to 0, im assuming thats because my if statement is always being true but i cant figure out the work around to tell it to only be true if it finds that string, my code is below, I know string::npos is something that can be used to determine if it finds the string im looking for with .find().
if(html.find("Quote Not Found page for WSJ Market Data") != string::npos)
{
peRatio = 0;
pcfRatio = 0;
pbRatio = 0;
psRatio = 0;
qtrEpsEstimate = 0;
annEpsEst = 0;
qtrLastYear = 0;
annLastYear = 0;
totalCash = "";
totalDebt = "";
totalLiabilites = "";
bookValueTotal = 0;
totalDebtEquity = 0;
totalDebtCapital = 0;
totalDebtAssets = 0;
interestCoverage = 0;
lastReport = "";
nextReport = "";
fiscalEnd = "";
}
else
{
I want to have an if else statement inside a loop when getting the results of
data from mysql .. However the if statement cannot read the result.. but the codes are perfectly I see the problem in my if else condition.
here is my Code from my asynctask
for (int i = 0; i < markers.length(); i++) {
JSONObject c = markers.getJSONObject(i);
// Storing each json item in variable
Double LAT = c.getDouble(TAG_LAT);
Double LNG = c.getDouble(TAG_LNG);
String color = c.getString(TAG_STATUS);
String red = "ongoing";
String green = "firedout";
if (color == red){
LatLng position = new LatLng(LAT, LNG);
status.add(new MarkerOptions()
.title(color)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
.position(position));
// adding HashList to ArrayList
}
if (color == green){
LatLng position = new LatLng(LAT, LNG);
status.add(new MarkerOptions()
.title(color)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
.position(position));
// adding HashList to ArrayList
}
}
Equivalent String values are not guaranteed to be unique in Java. In other words, there can be two String objects with exactly the same value.
String s1 = "ongoing";
String s2 = new String(s1);
System.out.println(s1 == s2); // false!
System.out.println(s1.equals(s2)); // true :)
Therefore, you must say:
if (red.equals(color)) {
// do something
}
I have the following code:
using (CPASEntities ctx = new CPASEntities())
{
IWorksheet ws = wb.Worksheets[0];
ws.Name = "Summary";
var tsm = (from x in ctx.tblTimesheetMasters
where x.TSID == TSID
select new
{
TimesheetID = x.TSID,
Comments = x.TSComments,
Vendor = x.tblVendor.Vendor_Name,
StartDate = x.TSStartDate,
Author = x.TSAuthor,
Approver = x.TSApprover,
Override_Approver = x.TSOverrideApprover,
Status = x.tblTimesheetStatu.TSStatusDesc
}
).ToList();
SpreadsheetGear.IRange range = ws.Cells["A1"];
// I want to copy the entire tsm list to this range, including headings.
}
As the comment states, I want to put that entire list into the ws worksheet starting at A1. I include the code in case it's easier to use a different construct. FWIW, there will be only one entry...TSID is the primary key. I can, of course, use the .FirstorDefault() construct if that is important. I thought it not important.
Your range is only one cell. You need a range big enough to contain all the cells the list would populate.
To populate your worksheet with the list, you could do something like this.
int iRow = 0;
int iCol = 0;
if (tsm.Count() > 0)
{
foreach (var prop in tsm[0].GetType().GetProperties())
{
ws.Cells[iRow, iCol].Value = prop.Name;
iCol++;
}
iRow++;
foreach (var t in tsm)
{
iCol = 0;
foreach (var prop in t.GetType().GetProperties())
{
ws.Cells[iRow, iCol].Value = prop.GetValue(t, null);
iCol++;
}
iRow++;
}
}
If you want a range, you could add this line.
SpreadsheetGear.IRange range = ws.Cells[0, 0, iRow - 1, iCol - 1];
I have a group of checkboxes representing the days of a week in my Qt application GUI and I select one or many days, and depending on which boxes are checked, pass a query string to PostgreSQL in order to display certain data on those days -e.g. if I checked monday and wednesday, extract (dow from timestamp) = 1 or extract(dow from timestamp) = 3 should be added to my query. I have just typed a crude solution -though haven't tested yet as I write this-, but I was wondering if there is a shorter -and more elegant- approach that I'm missing out here. The code is as below: -the queryAdditionCalltimePart and queryAdditionCallStampPart strings are later added to the relevant parts of my main query's QString before the main query is executed-
bool checkboxArray[7];
bool mult = false;
checkboxArray[0] = this->ui->checkBoxMonday->isChecked();
checkboxArray[1] = this->ui->checkBoxTuesday->isChecked();
checkboxArray[2] = this->ui->checkBoxWednesday->isChecked();
checkboxArray[3] = this->ui->checkBoxThursday->isChecked();
checkboxArray[4] = this->ui->checkBoxFriday->isChecked();
checkboxArray[5] = this->ui->checkBoxSaturday->isChecked();
checkboxArray[6] = this->ui->checkBoxSunday->isChecked();
QString queryAdditionCalltimePart = "";
QString queryAdditionCalStampPart = "";
int count = 0;
queryAdditionCalltimePart.append("(");
queryAdditionCalStampPart.append("(");
for(int i = 0; i < 7; i++)
{
if(checkboxArray[i] == true)
{
count++;
}
}
int x = 0;
for(int i = 0; i < 7; i++)
{
if(checkboxArray[i] == true)
{
queryAdditionCalltimePart.append("(SELECT EXTRACT(DOW FROM calltime) = '" +QString::number(i+1)+"')");
queryAdditionCalStampPart.append("(SELECT EXTRACT(DOW FROM cal.stamp) = '" +QString::number(i+1)+"')");
}
if(count > 1 && checkboxArray[i] == true)
{
if(x == count - 1)
{
}
else
{
queryAdditionCalltimePart.append("OR");
queryAdditionCalStampPart.append("OR");
x++;
}
}
}
queryAdditionCalltimePart.append(")");
queryAdditionCalStampPart.append(")");
You can add properties to any widget in Qt, http://qt-project.org/doc/qt-4.8/qobject.html#setProperty. The property can have any information that you want.
In your particular case, it would be cleaner to attach the SQL string as a property for each checkbox.
this->ui->checkBoxMonday->setProperty("sql",
"(SELECT EXTRACT(DOW FROM calltime) = '" +QString::number(i+1)+"') OR ";
Once you receive the user input, simply append the check box properties and remove the final OR.