C/C++ :: Sorting A File Based On ASCII Values?

Sep 1, 2014

New file_sorter.c:

#include <stdio.h>
#include <stdlib.h>
#include "sort.h"
#include <string.h>

[Code].....

My issue is now a segmentation fault in my sorting algorithm. In my assignment I was suppose to implement quick and insertion sort for the file, and organised strictly by ASCII values. We were allowed to look up generic algorithms for the sorts, but we have to convert them to comparing char arrays.

I haven't started trying to configure quick sort yet, but I'll supply the generic algorithm as well. keep in mind that the first line in the file contains the number of lines in the file. I'll try not to disappear this time!

sort.c :
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

[Code]....

View 4 Replies


ADVERTISEMENT

C++ :: Sorting Some Data Based On Values Of A String Of Bits

Apr 1, 2014

I have a problem I am working on where I need to sort some data based on the values of a string of bits. The strings look like this,

010000001110000000

there are 18 bits, 1 means a feature is present, 0 means the feature is absent.

Each of these string has 4 on bits. I need to sort them such that I have the longest possible runs with 3 of the same on bits. It doesn't matter which 3 bits are on, I am just looking to order them in blocks with the longest possible runs. As a second step, the ordered blocks will be sorted by size large>small.

The following data is ordered like I need it to be.

Code:
// block 1, run of 12, keys 1,2,11 are identical (key 12 is also identical)
011000000001100000
011000000001100000
011000000001100000
011000000001100000

[Code] .....

This is the sort order that I am looking for. I need to be able to take a list of the bit strings in any particular order and sort them into the order above. The algorithm would need to recognize that there are 4 on keys and then look for groupings of three common on keys.

This is more of an algorithm question than one about specific implementation in code. I generally assume that most programming problems have been solved one way or another, so I don't know much about analyzing and manipulating strings of bits.

Is there a standard method for this kind of pattern recognition?

View 14 Replies View Related

C :: Load Up File That Contains Series Of Int Values Stored In ASCII

Sep 23, 2013

This seems like a fairly straight forward assignment. Load up a file that contains a series of int values stored in ASCII and print out the ASCII characters to the console.

The problem I am having is that I am getting the numerical value of bytes ("40" for 10 numerical values, "200" for 50 values). The numbers are generated randomly by another file, but I can control how many numbers are generated. For example, if I type in:

shell% cat tempfile

the output is as follows:

/8?qE?. Y4?(T???a???%@

but when I try to run it with my program:

shell% ./intcat tempfile

the output displayed is:

40

Code:

#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
//check if arguments match

[Code] .....

View 10 Replies View Related

C++ :: Sorting Alpha Characters Not ASCII?

Nov 7, 2013

I am looking for a way to sort a string just by the alpha characters, not by the ASCII table. Therefore

string sort(string name) {
sort(name.begin(), name.end());
return name;
}

will not work. And I am looking for the most simple way to go about it!

View 7 Replies View Related

C/C++ :: Sorting Characters In Their ASCII Codes

Mar 25, 2015

I'm trying to write a lot of sample code to practice but I can not figure out how to take a string and sort each character in order of their increasing ASCII codes. I'm getting stuck trying to separate each letter to determine it's ASCII code. I know I have to use an array somehow, So far, this is my code:

#include <stdio.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS

[Code].....

View 4 Replies View Related

C :: Sorting Has To Be Based On Int Field

Feb 16, 2013

I have a file which has records with 2 fields--one int and one float..what is the best way to sort it?

The sorting has to be based on the int field(the first field) (each time the program runs the file might end up with some hundreds of records).

View 13 Replies View Related

C :: Drawing A Box With Coordinates - Printing Between Certain ASCII Values

Sep 23, 2014

The assignment is:

1) Write a program that asks the user for a a single character and two XY coordinates. The two X and two Y values should all be integers between 0 and 50. The character should be a printable ASCII character with values between and including ' !' (ascii value 33) and '~' (ascii value 126).

2) Your program should then draw a rectangle made up of the user selected character where the upper left corner is at X1; Y 1 and the lower right corner is at X2; Y2. Be sure to print the appropriate number of blank lines (having spaces in the blank rows is OK) in the beginning and pad each row of your rectangle with X1 leading spaces.

The Output is supposed to be similar to this:

(X1,Y1) = (0,0) , (X2,Y2) = (4,4), the character = ^

^^^^
^^^^
^^^^
^^^^

What I am having trouble understanding is printing between certain ASCII values (ASCII has never been discussed in class).

Another thing I am having trouble with is the main part of the assignment. From what we are currently discussing is loops and the assignment is covering nested loops. My code looks similar to this:
Code:

#include <stdio.h>
int main (void) {
int X1, Y1, X2, Y2;
char cRec;
printf("Enter a character: ");
scanf("%c", &cRec);

[Code] .....

My thinking on the assignment is that you want the X1 coordinate to increase to the value of X2 (same for Y1 and Y2). Is this thinking wrong?

View 12 Replies View Related

C++ :: Sorting (array Based) Database?

Mar 26, 2013

knows if there is a ready made function in C++ to sort (numerically or alphabetically) multiple arrays based on a particular array?

i.e. Imagine you have multiple arrays or a multi-dimentional array containing data about users (name, age, etc.)

Is there a function that for example can sort it by name, and automatically shift the age so they still correspond to the correct user name?

View 2 Replies View Related

C :: Sorting List Of Students Based On Their Test Scores

Jan 18, 2014

I need to sort these students based on their test scores.

My biggest problem is with my sort function. I am not sure of the syntax to call the items the proper way. I also don't know how to call the function properly as I know that i can't pass a int to a struct. Also not sure about the z malloc. Just threw that one in there.

Code:
struct student{
int id;
int score;
};
void sort(struct student* students, int n){
/*Sort the n students based on their score*/

[Code] ....

View 5 Replies View Related

C++ :: Sorting Vectors Based On Value Of First Integer In Ascending Order

Feb 13, 2013

I have a vector which contains vectors containing 7 integers each. I'd like to sort these vectors based on the value of the first integer (int IOT), in ascending order. I know this type of question can be found everywhere, but I'm lost as to why this doesn't compile.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <windows.h>
using namespace std;
class orders {
public:
int IOT; // Incoming Order Time

[Code] ....

View 7 Replies View Related

C++ :: Sorting Data Based On Some Metric - Function Pointer Syntax

Nov 20, 2014

I have a piece of code that sorts data based on some metric. The some metric is something I now want to make flexible so I can easily switch between and compare metrics. To do this, I want to pass the function to use as a parameter to the method that does the sorting (and other stuff). However, I'm having problems figuring out the syntax. Also, I think my [temporary] organization of code is violating a lot of basic code design principles.

To make the function pointer passable, I defined the "typename" in the header where the function is located (it is part of a struct, "Data"):

// Below the struct definition of Data
typedef double (Data::*CostF)(unsigned l, double W) const;

The two example functions I want to use are defined in that struct ("Data"):

// Inside the struct definition
inline double someExampleCost(unsigned l, double W) const {
// Returns some basic calculation
}

The function that uses it is part of a different class (that holds a reference to the first class, in case that matters; I feel like I'm doing something odd here, because I'm binding a member function in the definition/passing, but never referencing the object). It looks like this:

// Inside another class ("Foo")
inline void DoSomeStuff(double& ECost, double& TCost, CostF cost) {
// Irrelevant stuff here
std::sort(vector.begin(), vector.end(), [&](unsigned a, unsigned b){
return (*cost)(a, W) < (*cost)(b, W);
});
// More irrelevant stuff here
}

The error shown is "operand of "*" must be a pointer". If I remove the '*':
[code]return cost(A, W) < cost(b, W);

the error becomes: "expression must have a (pointer-to-)function type."

The call to this function is, currently, just in the main function, as I'm just testing before I wrap it into real code. It looks like this:

// In main
Foo bar; // Make an object of the struct that has the "sorting" function
CostF costFunction = &Data::someExampleCost;
// Bind to the Cost function bar.DoSomeStuff(varA, varB, costFunction);

This bit shows no errors by itself. So, my questions:

a) Clearly I'm missing the insight into Function Pointers. I'm comfortable with regular pointer stuff, but I can't wrap my head around FPs, partly due to the awkward syntax.

b) I'm very uncomfortable with the fact that I'm binding a member function of a class, but never try to reference an actual object of that class. This is probably a big part of why it's not working, but I can't seem to bind a function belonging to a specific object. I thought of doing

// In the main again
Data d; // Construct the object, which contains big lookup tables
Foo F(d); // Construct the object, which only holds a reference to a Data object
CostF costFunction = &d.someExampleCost; // Bind to the Cost function of that object

but that doesn't work ("a pointer to a bound function may only be used to call the function").

View 4 Replies View Related

C :: Read A Line From Screen / Takes ASCII Values From Entered Text And Edit It

May 10, 2013

This is a small program that reads a line from the screen, takes the ASCII values from the entered text, and edits it. Next, the ASCII values are reinterpreted (am i misspelling?) as text, and displayed. However, my final output has (allways?) at least 7 characters, even if you enter only one. I'm also trying to accomplish, that the part of the string which isn't filled, will be printed empty. In spaces.

compiled with -std=c99.

Code:
#include <stdio.h>
int main(){
int maxsize;
printf("How long is your message?

[Code] .....

View 6 Replies View Related

C++ :: Item Search Based On 2 Values For Index?

Apr 4, 2013

I have to be more code specific (I usually try to abstract my code). As you can see I use a lot Qt classes... however they work very similar to standard libraries, so it should be not a problem to look at this question.

class FrameManager {
QList<FRAME> frameData;
public:
int frameID(int grpno, int imgno);
};

[Code] ....

Now I know the example search only the ID and doesn't allow the access of the actual data... this is not the question.

The problem is... I will need to define an AnimationManager class that will collect sequence of frames (identified by the unique couple of value) in animations... so it will need to use a massive search of item from the couple of value (and not ID, unluckly)

I fear if I use an approach similar to the one written in the example (search from the start to the end until the element is found) can be very unefficient.

In the same time the AnimationManager should always know if frame exist or not (if not exist an invisible image will be shown) and if in the meantime changed.

Another problem is that I cannot order the data sequence inside FrameManager (becouse it is expected to mantain the original order, even when it is chaotic).

I tried to take a look around QMap (or std::map) but it is not clear at all how the optimiziation works and how I can use it in my case

I tried also to take a look at the "hash" concept, but for me it is too complex to understand deeply.

So... I am feeling like I am entrapped... I am unable to find a good "exit"...

View 8 Replies View Related

C++ :: Incrementing Values In Vector Based On Variable Input

Mar 24, 2014

I'm trying to increment the values in a vector, not the vector size, based on variable input. Basically I have a vector of size 10, and all of its values are initialized at zero. The program counts the frequency of numbers 0-9 in a four digit user input. This is what I have (I want it to work so badly but the compiler says that I'm using a pointer to a function used in arithmetic):

for (int i=0; i < num_slots; ++i) {
++guess_frequency[guess[i]];
}

I just want to know if you can increment values within a vector:

E.g.
change
0 0 0 0 0 0 0 0 0 0
to
1 0 0 0 2 0 0 1 0 0

View 6 Replies View Related

C++ :: Create Matrix Of Data That Add Values Based On Reading Get From DVM

Apr 22, 2012

Ok I'm trying to create matrix of data that I can add values to based on a reading that I will get from a DVM. I'm using a 3d vector to do this. I need to keep track of the serial number, the day and within day I need to take two readings at different temps, this will continue for 7 days.

The rows of my 3d vector will be the days, the colums will be the serial numbers the depth will be the reading at the different temps.

What I need to do is compare the first element (days) and when it is greater then or equal to 4 I will perform calculations of the readings. So all I want to do is compare the first element of the vector, How do I do this?

Here is what I got

Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
typedef std::vector<Double2D> Double3D;
#define HEIGHT 5
#define WIDTH 3

[Code]....

View 7 Replies View Related

C++ :: Sorting Values In Array?

Feb 8, 2015

I tried sorting arr2 from lowest to highest value, but it only gives me 4 values and then the rest are zero.

Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << fixed << setprecision(1);

[code].....

View 1 Replies View Related

C++ :: Create A File Which Containing Letters Related With Above ASCII?

Aug 30, 2014

I have a text file which contains ASCII of A,B and C like below,

65
66
67

Now I need to create a file which containing letters related with above ASCII like below, ABC

This is my code.

#include<iostream>
#include<fstream>
#include<string>

[Code]....

View 2 Replies View Related

C++ :: Sorting Elements Of Array In Order From Highest To Lowest Values

Jul 6, 2013

I am writing a function called swap that sorts the elements of an array in order from highest to lowest values where they descend and ascend. A particular thing about this function is that swap does NOT re-arrange elements in the array so it just uses a second array of indexes for the elements in the original array and then swap sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes. You can declare and initialize the original array without any user input.

For example, int arr[10] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 };

Header of the function swap must be as shown below: void swap(int array[],int swapedIndexes [], int size, char mode) When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order. int swappedIndexes[5];swap(array,swappedInde… 5, 'a');

So far I have this code that randomly generates arrays of 10 elements and I found the max and min, but I am confused on what to do about the swapping part? Also, how to make the numbers ascend or descend in numerical order?? Here is what i have done so far:

#include<ctime>
#include <iostream>
using namespace std;
int min(int [],int);
void max(int [],int , int &);
void main()
{ srand(time(0));
//1-declare

[Code] .....

View 1 Replies View Related

C++ :: Find Non Printable ASCII Value - Using Library File Unittest

Jan 3, 2014

My program is find non printable ASCII value(1 to 32) and calculate count from a text like "hi<tab>this<space>my text.i'm using library file here.(visual studio 2012)

removeascii.h(program name)
#pragma once
#include<stdint.h>
#include <string>
using std::string;
namespace ascii

[Code] ....

My ERROR is:

1>c:program files (x86)microsoft visual studio 11.0vcunittestincludecppunittestassert.h(65): error C2338: Test writer must define specialization of ToString<const Q& q> for your class class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >

[Code] ....

View 1 Replies View Related

C :: Naming Output File Based On Input File Name

Sep 7, 2013

I have a program where I will read in a certain .txt file, such as "financial_data.txt" and I will be doing some sorting with this data.

I want my program to write out the sorted data to an output file and I want the name of the output file to be based off of the input name. For example, since my input file is "financial_data.txt", I want the output file name to be "financial_data_out.txt".

I am having a hard time finding examples online and in my reference manuals...

View 4 Replies View Related

C++ :: Hash Table Program - Sorting Pointer Table After All Of Values Entered

Nov 19, 2013

I am having an issue with my sort function. This is one part of the Hash table program. The main issue is that I am trying to sort the pointer table after all of the values have been entered. The ptr_sort function is not a class function and so I am therefore unable to use the class variables psize and pTable. Is there another way I should be trying this? it is a Vector should I use the sort() function from the vector class in STL?

#include "/home/onyuksel/courses/340/progs/13f/p9/util9.h"
#include "/home/onyuksel/courses/340/progs/13f/p9/hTable.h"

#ifndef H_TABLE1
#define H_TABLE1
void ptr_sort ( );

[Code] ....

View 1 Replies View Related

C :: Set Variables Based On File Read?

Dec 9, 2013

I am trying to fscanf a file and need to read the variable name 'f9' and 'a2' for example in the sample below and get their corresponding values '8' and '2' and then in my C program set the variables f9 and a2 accordingly (which I can declare as variables). Is this possible?

I making a Sudoku Solver and am thinking I would store the puzzle solution in a 2D array with a1=0, a2=1, etc... where 0, 1 are the array indices... then just print the array as the solution.

Note: The a1, a2, a3, etc... is my own numbering of the indices where the letter corresponds to the row and the number corresponds to the column (starting from 1) which I use when feeding to the solver.

I have a file that has the following format (output of Z3 SMT solver):

sat
(model
(define-fun f9 () Int
8)
(define-fun a2 () Int
2)
(define-fun c5 () Int
6)
)

View 1 Replies View Related

C++ :: Create A Binary File Based On HEX Data

Apr 24, 2013

I want to create a binary file based on some hex data :

Ex Input Hex :54313032202020303030

Out put like :6      

View 2 Replies View Related

C# :: Generating And Populating XML File Based Off XSD Schema

Jan 14, 2014

The scenario is that I have an XSD schema; I have generated a C# class using the XSD tool in the Visual studio command line.

I have then created a basic console app, which will populate the first partial class in the generated C# class.

The program will then serialize this data and create an XML in a folder as a proof of concept.

The issue I'm having is working out the best way to populate and serialize all the data from the class, as the XSD it was generated from is quite large.

I would like to be able to randomise the data at a later stage once I have the basic populating and serializing working.

View 6 Replies View Related

C++ :: Pointer-based Data Loading From A (text) File

Dec 4, 2013

I have two classes, productListType and buyerListType, that are each basically linked lists. To create the linked list for an object of productListType, I successfully wrote a non-class function createProductList to read the product data from a text file. The class definition for buyerListType has a data member productBoughtRecord that is of type productListType to aggregate the details of the products purchased by a particular buyer during transactions. I decided to make productBoughtRecord a pointer since the contents of this data member would wax and wane over the course of several transactions, depending on the amount and frequency of payments made by the buyer. I have provided a rough sketch of the class below:

class buyerListType
{
public:
setCustomerInfo( ....., productListType* p);
.
.
.
private:
productListType* productBoughtRecord;
.
.
};

I'm similarly trying to write a non-class function createBuyerList to load the record of customers from a text file. How do I determine what the value of the formal parameter p in member function setCustomerInfo is, in order to be able to set the data member productBoughtRecord? take into consideration that an object of class buyerListType would have multiple buyers with varying amounts of products purchased.

View 11 Replies View Related

C/C++ :: Reading From File To Construct A Pointer Based Maze?

Nov 29, 2014

I'm trying to read a "pointer-based" maze .txt. The first letter in each row corresponds to the room letter...then the letters that follow are North node, East node, South node, and West node respectively. The asterisk indicates an empty room or not a valid option.

Here is what I have come up with, what is happening is after the file is parsed by read_maze it is calling my is_empty function indicating that there is no maze because it doesn't go into the else statement here.

I've attached a sample input file:

 maze.txt (130bytes)
Number of downloads: 19

We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.

void Maze::read_maze(string FileName){

string line;
ifstream inStream;
inStream.open(FileName.c_str());
int test = inStream.peek();
int i = 0;
if (!(inStream.fail())){
while (!inStream.eof() && test != EOF){

[code]....

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved