C :: Sort Lines Of Matrix In A Single Function

Jan 14, 2014

I need to write a code (in C99). The programe receives an input number (integer) 'n' and then a matrix sized n*n (matrix[n][n]), all numbers are integers. We're supposed to define the matrix's size with malloc. Now the program needs to sort the lines of the matrix (the number in each line), in a single function (!) this way:

even line index (0,2,4,6...): from small to big.
odds line index (1,3,5...): from big to small.
and then print it.
*note: first line is indexed 0, second line is 1, etc.

I was thinking to sort it with bubblesort function with the following if:
if(i%2==1)
do odds sorting.
else
do even sorting.
when i is the index of the row.

my problem is defining the malloc and how do I send the matrix to sorting.If needed I will attach my current (not so good (completly awful)) code and functions as well as an example of what the prog. supposed to do.

View 13 Replies


ADVERTISEMENT

C :: Sort Matrix Such That It Has Value In Ascending Order

Sep 29, 2013

Trying to sort matrix such that it has value in ascending order.

But outcome is coming :

Code:
Matrix after sorting :
2,2,2,2,
2,2,2,2,
2,2,2,2,
2,2,2,2,
Can't find whats wrong.

[Code]....

View 2 Replies View Related

Visual C++ :: Creating Matrix / Map For Topological Sort?

May 10, 2014

I am trying to simulate the logical circuits having NAND gates. I am reading the structure from .bench file. The structure of a circuit looks like below:

Code:
INPUT(1)
INPUT(2)
INPUT(3)
INPUT(6)
INPUT(7)
10 = NAND(1, 3)
11 = NAND(3, 6)
16 = NAND(2, 11)
19 = NAND(11, 7)
22 = NAND(10, 16)
23 = NAND(16, 19)

It can be seen that the inputs for Gate 22 are 10 and 16 which are also NAND gates. On the other hand, for gate 10, the inputs are 1 and 3 which are simple inputs. So, i need to sort the gates topologically. Although topological sort will not affect the output for this particular example but i have big circuits also in which topological sort is required. But we can use this simple example to do the topological sort.

My effort:I thought to create a map which will keep the information about the gate name and its indegree. But i am unable to proceed further.

Code:
void Circuit::topologicalSort()//topological sorting (for the mixed circuits) {
int counter = 0;
std::map<string, int> unsortTopoList; //this list keeps a track of gate name and its Indegree
for(int i=0; i<gates.size(); i++) {

[code].....

View 4 Replies View Related

C :: Radix Sort Function To Sort Array Of 64 Bit Unsigned Integers

Aug 19, 2013

Example radix sort function to sort an array of 64 bit unsigned integers. To allow for variable bin sizes, the array is scanned one time to create a matrix of 8 histograms of 256 counts each, corresponding to the number of instances of each possible 8 bit value in the 8 bytes of each integer, and the histograms are then converted into indices by summing the histograms counts. Then a radix sort is performed using the matrix of indices, post incrementing each index as it is used.

Code:
typedef unsigned long long UI64;
typedef unsigned long long *PUI64;
PUI64 RadixSort(PUI64 pData, PUI64 pTemp, size_t count) {
size_t mIndex[8][256] = {0};
/* index matrix */
PUI64 pDst, pSrc, pTmp;
size_t i,j,m,n;
UI64 u;

[Code]....

View 9 Replies View Related

C++ :: How To Sort A 1D Array Using Function Sort

Mar 22, 2013

how to sort a 1D array using function sort().but if i have a 2D array how do i sort only 1 row of it (or column)?

View 2 Replies View Related

C++ :: Return 3 Values From A Single Function

Jun 7, 2013

I've got the program for the most part except one part because it's basically wanting me to return 3 values from a single function and I'm unsure how to do this the way it wants me to. The rules:

Call the user-defined function to read in x in the series to be used for calculating the results. Pass a prompt for x as an input parameter, and return the validated x value to main.

After a valid x has been entered, call the same user defined function a second time, to read in y. Pass the prompt for y as an input parameter, and return the validated number of terms value to main.

After a valid y has been entered, call the same user-defined function a third time, to read in z. Pass the prompt for z as an input parameter, and return the validated z value to main.

View 2 Replies View Related

C/C++ :: How To Write A Function For A Single Turn Involving Die In A Game

May 16, 2014

So basically it consists of implementing a single turn for the game called 'pig' and printing out scores and probabilities of those scores. So this is what I have thus far :

int randomNum (int min, int max) {
return min + rand () % (max - min + 1);
} int singleTurn (int holdValue) {
int totalRoll = 0;
int score = 0;
do {
score = randomNum(1,6);

[code]....

View 13 Replies View Related

C :: Function That Will Work For Both Dynamic And Static Implementations Of A Function To Get Transverse Of Matrix

Feb 11, 2013

i need a function that will work for both dynamic and static implementations of a function to get the transverse of a matrix. so far, i have this

Code:

matrix transpose(matrix m)
{
int row, col;
row = m.com_dim;
col= m.row_dim;
}

[code]....

this works well with my static implementation, but when i try it in dynamic it gives me errors. the function has to be the same for both dynamic and static implementation

View 4 Replies View Related

C/C++ :: Function Output For Three Lines

Nov 23, 2014

I am doing a written lab in my programming class in which we must write the output for three lines in a function. However, when I enter the code in my compiler I only get error messages. I was just wondering what the outputs under snap, crackle and pop should be and why.

#include <iostream>
using namespace std;
void snap (int i, int j);
void crackle (int &a, int &B)/>;
void pop (int &e, int f);
int main () {
int i = 1, j = 2;

[Code] .....

View 4 Replies View Related

C++ :: Lines Without Semicolon - Function Calls?

Aug 28, 2013

The below code is taken from open source filezilla project

(FileZilla_3.7.3_srcfilezilla-3.7.3srcinterfacebookmarks_dialog.cpp)

Code:
#include <filezilla.h>
#include "bookmarks_dialog.h"
#include "sitemanager.h"
#include "ipcmutex.h"
#include "themeprovider.h"
#include "xmlfunctions.h"
BEGIN_EVENT_TABLE(CNewBookmarkDialog, wxDialogEx)
EVT_BUTTON(XRCID("wxID_OK"), CNewBookmarkDialog::OnOK)
EVT_BUTTON(XRCID("ID_BROWSE"), CNewBookmarkDialog::OnBrowse)
END_EVENT_TABLE()

I'm unable to understand what are these lines after the #include. They don't end in semicolons. Looks very much like function calls.

Are BEGIN_EVENT_TABLE, EVT_BUTTON and END_EVENT_TABLE function calls? Function calls should end with semicolons right?

View 5 Replies View Related

C++ :: Syntax Error In Function Call - Type Mismatch In Parameter In Function Sort

Jul 6, 2014

error says "cannot convert 'int*' to 'int' in function main()
and also
type mismatch in parameter in function sort(int,int)

Heres the code:
#include<iostream.h>
#include<conio.h>
void main() {
void sort(int,int);
clrscr();

[Code] .....

View 11 Replies View Related

C :: Counting Function - Prints All Input Lines That Are Longer Than 80 Characters

Jan 10, 2014

I'm doing an exercise that prints all input lines that are longer than 80 characters. I rather not use any libraries so I decided to write my own function that counts characters to use it in my main program. However when integrate things my function returns zero all the time.

Here is my full code:

/* Exercise 1-17 Write a program to print all input lines that are longer than 80 characters */

#include<stdio.h>
/* Declarations*/
#define MAX_STRING_LEN 1000
int count_characters(char S1[]);
int main() {

[Code] .....

So I was trying to debug my count_characters() function and this is the code if I was to run it seperately:

Code:
#include <stdio.h>
/* counts character of a string*/
main() {
int nc = 0;
int c;
for (nc = 0; (c = getchar()) != '
'; ++nc);
printf("Number of characters = %d
", nc);
}

which works...

View 1 Replies View Related

C++ :: Function Outputs Different Matrix

Jun 25, 2013

It is getting more and more annoying, everytime i progress the next hurdle is just bigger. The problem is i want to calculate inverse of a matrix but using .inv gives a matrix full of -1.#QNAN values so i decided to write one my self

a function which returns the inverse matrix but something strange happens.

when i debug through the function code it works well and check the matrix that will be returned and the elements are correct.

when the function is called and output matrix is resulted. the element of the matrix is different.

matrix = inversemat(matrx)
inside function, invmat= 3 1 2, 3 5 1, 8 1 2
outside the function matrix = -9.1249e+61 wtf.

here is the code

int main() {
double matrx[3][3]={{1,2,3},{0,1,4},{5,6,0}};
Mat matrx1(3,3,CV_64F,matrx);
Mat inmat;
inmat=inversemat(matrx1);

[Code] ...

invmat elements are correct, as for inmat the elements are too small -9.212412e+61

What is going on, when the matrix is returned.

View 19 Replies View Related

C++ :: Return Matrix In Function

Jul 4, 2013

How we can return a 3x3 matrix in c++ function.

My code is:

double *computeA() {
double *A = new double[2][2];
// some operations on A and then return
return A;
}

The error that I have in this case is:

error C2440: 'initializing' : cannot convert from 'double (*)[2]' to 'double *'

View 7 Replies View Related

C :: Function Capable To Invert Matrix Of Integers

Jun 4, 2014

I am looking for function capable of converting square matrix of integers.Where to find such function? I know the algorythm but have no time to implemnt it.

View 12 Replies View Related

C++ :: Changing Values In A Matrix Using Void Function

Jun 29, 2013

I have the following void function devised to assign "+1" or "-1" to each element of a matrix at random. While the function does what I want, when I print the values of the matrix all are set to zero:

#include <vector>
#include "aRand.h"
#include <iostream>
void initConfig(std::vector<std::vector<int> > premat, int nL, int nN) {
int * pnRand;
pnRand = 0;

[Code]...

The function pnRand_plus returns a pointer to an array of random numbers from 1 to 100, with seed time(NULL) + i. The values printed in main are zero, despite the values printed during the function run are fine (-1s and +1s).

View 2 Replies View Related

C++ :: How To Use Bool Function To Identified A Identity Matrix

Dec 10, 2013

how to use bool to identified a identity matrix

View 7 Replies View Related

C++ :: Using OOP To Implement Matrix Class That Provide Basic Matrix Operations

Mar 27, 2013

i want to know how i can solve this question? do i need to create a class or write the program codes.

View 12 Replies View Related

C :: How To Make A Sort Function

Feb 28, 2013

Im suppose to make a "poor mans" variation to the Sort function built into unix. The program is suppose to read in a file and sort the contents of the file. So its a variation of the Unix Sort feature. I have remade the readLine function we were provided so that it doesnt use fgets. where to go from here, Not sure on how to make a sort function. Here are the reqirements of the program:

Code:

• Re-implement the readLine() function so that it no longer makes use of fgets(). Instead,
process the input on a given line character-by-character.

• Provide code which will create a data structure similar to argv to hold all of the words to be sorted. Use malloc() to ensure that each entry has just the required number of bytes needed to store the words. The final entry in your array should be the NULL pointer.

• Implement a sort() function which will rearrange the words in sorted order. To swap two words in your array, note that only a pair of pointers need to move. The strings themselves, once established, will never move. Heres what i have:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINES 1000 /* maximum number of reminders */
#define WORD_LENGTH 10 /* max length of reminder message */

[code]....

View 9 Replies View Related

C :: Multiplication With Sort Function

Oct 30, 2014

I need to include validation to only accept numbers and to only the last four odd numbers in the table.

Code:

#include <stdio.h>void main() {
int upper, range, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&upper);

[Code].....

View 3 Replies View Related

C :: Matrix With Zero And Nonzero Elements - Function That Returns 3 Arrays

Mar 17, 2013

I have a matrix that contains zero and nonzero elements. I want to do a function that return 3 arrays.

The first one is for nonzero elements the second array contains the corresponding row numbers of each nonzero element the third array contains the corresponding column numbers of each nonzero element.

View 11 Replies View Related

C/C++ :: Parallelize Matrix Multiplication Function Dynamically And Statically

Oct 1, 2014

I've been trying to get my matrix multiplication program to run a few different ways. My assignments wants me to run it statically using chunks, but we're not supposed to use OpenMPs scheduler. So I'm not sure how that's possible. And secondly, we have to run it dynamically using locks/unlocks.

Static (not sure how to implement chunk)

#pragma omp section
{
printf("Thread %d doing matrix mult", threadID);
if (matrixAcols != matrixBrows) {
cout << "Error, operation not possible" << endl;

[Code]....

The code will run for a long time too. It uses as many threads as we tell it to but it never speeds up the results.

Quote
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 2 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult
Thread 4 doing matrix mult

View 2 Replies View Related

C++ :: Accept Matrix As Argument And Display Its Multiplication Matrix

Jun 1, 2014

I just want to know the code of the program: Write code to accept matrix as aurgument and display its multiplication matrix which return its multiplication matrix.

View 1 Replies View Related

C++ :: Standalone Sort Function For The Class

Apr 30, 2013

I need to write a standalone sort function, which will take a linked list parameter and sorts it. The function is included in the main list class as friend class: Code: friend void sort (UList<T>&) I need to submit separate SortIS.h file for insertion sort. Here is my code:

Code:

#include <iostream>
template <class T>
void SortIS(UList<T>& list)
{
T temp;
for (int i=1; i<list.size(); i++)
{
temp = list[i];

[Code]...

But the teacher said it didn't compile returning an error: "undefined reference to `void". What I am missing here?

View 6 Replies View Related

C :: Sort Array By Function To Get Max Number

Mar 6, 2015

I have this code(homework) i've been working on for several days i couldn't fix this error.

Question is: to write a sort program in c . Ineed to ask user for how many numbers to be sorted(n) ask again for values. The approach of sort is this:

1- store n in temporary var (temp)
2- search for largest number in array (0 until temp-1)
3- switch the fied that store largest with field indexed by (temp-1)
4- temp-- (decrement)
5- repeat steps 2 to 4 until temp is 1

the program then prints the numbers of array on screen in one line. I also should use a function getMax(int *list , int n) that determines largest value and returns its location in that array

list : is the array
n: number of elements

Code:
#include<stdio.h>
int getMax(int *list, int n); //definition of getMax function
int main(void) {
int n,i;

[Code] .....

so i tried to sort this array

Code:
input 2 7 9 4 3 1 6 5
The out put should be : 9 7 6 5 4 3 2 1
instead it prints : 7 9 2 4 3 1 6

View 11 Replies View Related

C/C++ :: Sort String In Map Comparator Function

Jan 19, 2014

i am currently using a comparator function in STL map for sorting date of string data type in ascending value. My dates are in this format for e.g. 15OCT1990, 13SEP1980 and etc. I am using substring to split up the string so that i can compare day, month, year separately. Right now i have problem comparing the month portion because alphabetically "FEB" comes before "JAN".How can I make a fixed substring position of (2,3) which is my month value to accept that string value of JAN comes before FEB,AUG,DEC for e.g?

struct sortMapDaily: public std::binary_function <bool, std::string, std::string>
{
bool operator() (const std::string& lhs, const std::string& rhs)
{

[Code].....

View 5 Replies View Related







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