C :: Filling 2D Array Complexity?

Mar 24, 2013

Assuming that we have :

Code:
int arr2d[rows][columns] ; // Not valid syntax of course ... let be arr2d rows * columns size
for(int i=0; i<rows; i++)
for(int j=0; j<columns; j++)
arr2d[rows][columns] = some_value;

What is the complexity? I believe O(n) and not O(n^2) on this case because if you have 3*3 size you would put 9 elements (from 9 elements input of course)... for each input you have one insertion and that is the meaning. Same as 4*4 size 16 input times 16 insertions .. or 5*5 and so forth...

View 8 Replies


ADVERTISEMENT

C/C++ :: Running Array In Less Complexity

Feb 9, 2015

Giving a dynamic array, we want to pass the array elements from a file. The first number in the file N gives us the array length. They follow N numbers, the actual elements of the array.

Three brothers are going to work in the shop of their father for a time. The shop is doing well and every day generates profit which the three brothers may provide. The brothers agreed that they would divide the total time in three successive parts, not necessarily equal duration, and that each one will be working in the shop during one of these parts and collects the corresponding profit on his behalf. But they want to make a deal fairly, so that one received from three more profit someone else. Specifically, they want to minimize the profit the most favored of the three will receive.

I first created a program that WORKS! with complexity O(n3).

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int sum_array(int* array, int cnt){
int res = 0;
int i;
for ( i = 0; i < cnt ; ++i)

[Code] .....

Let's assume that we have the following input:

10

1 1 8 1 1 3 4 9 5 2

The ouptut should be 15 -> A = 1 + 1 + 8 + 1 + 1 + 3 = 15 , B = 4 + 9 = 13 , C = 5 + 2 = 7.

But the ouptut is 16!

How can I reduce the complexity of my first working! C code?

View 9 Replies View Related

C :: Filling 2D Array

Mar 24, 2013

So here is the C code:

Code:

#include <stdio.h>
int main(void)
{
//2D Array
int array[2][2];
int number = 1;
}

[code]....

The array is not filled incorrectly for some reason, more specifically the first row.The first two cycles of the for loop seem to work correctly. One if the bugs seems to occur on the third. when array[0][2] is filled with number 7, for some reason array[1][0] changes it value to 7 as well.

View 2 Replies View Related

C :: Filling Array With Even And Odd Number

Jun 25, 2013

I have this snippet :

Code:
#include<stdio.h>
#define LEN 3
int main(void)
{
int a[LEN] = {0} , b[LEN] = {0} , i = 0;

[Code] ....

/* OUTPUT :
a[0] = 2 , b[0] = 1
a[1] = 0 , b[1] = 3
a[2] = 0 , b[2] = 0 */

The problem is some elements of the array would be remained zero and unused. Is there any solution about this wasting of memory?

View 7 Replies View Related

C :: Filling Float Array

Oct 6, 2013

What am I doing wrong here.

Code:

#include <stdio.h>
#define N 25
int main () {
int s,min,sec,total,mins;
float speed,splits[N];

[Code] ....

View 2 Replies View Related

C++ :: Filling Int Array Using Binary File

Mar 2, 2013

I am trying to do binary filling. This is the Constructor code, you will see a couple of integer arrays:

Doc::Doc ( int useridValue , string firstnameValue , string lastnameValue , string deptValue
, int dayinhourValue[] , int dayinminValue[] ,int dayouthourValue[] , int dayoutminValue[]
, int timeperpatValue , int appValue[] , int applimValue[] ) {
setuserid(useridValue);

[Code] ....

it gives an error:

void Doc::setdayinhour ( intdayinHOUR[] ){
for(int i=0;i<7;i++) {
dayinhour[i]=dayinHOUR[i];//highlighted area
} }

I am using this code for filling:

void main () {
Doc r;
ofstream outDoc("docdata.dat" , ios::out | ios::binary);
if (!outDoc) {
cerr << "File Could Not Be Opened" << endl;

[Code] ....

View 5 Replies View Related

C :: Word Search Program - Filling 2D Array

Oct 5, 2014

I am currently working on writing a word search program. However, I am stuck on reading the used input into the 2-D array. The code I've posted below is only dealing with the user input (I'll work on the word search part once I know i am correctly reading in the user input). I know the coding is bad practice with the use of hexadecimal, and getchar() ect. But I am currently using a microblaze microprocessor and this is just the way microblaze can interpret the information. As for the infinite while loops...that can be changed just trying to figure out how.

My question is how could I change my code to correctly read in the user input into the 2-D array?

Code:
#include "platform.h"#include "xparameters.h"
#include <stdlib.h>
#include <stdio.h>
#define MAX 20
int main() {
char grid[MAX][MAX], word[30];
int i, j, arr[2],num;

[Code] ....

View 10 Replies View Related

C++ :: Filling Dynamically Allocated Array With Values

Jan 12, 2013

Just trying to fill a dynamically allocated array with values then I want to print out the values using pointer method:

#include <iostream>
using namespace std;
long * extend_arr(long int arr[], long int length, long int val) {
long * array2 = new long [length + 1];
for (int J = 0; J < length; ++J)
array2[J] = arr[J];

[Code] ....

When this runs, I get an array with random numbers in it. For example, just trying to print the first value in *Block gives me random numbers each time. What is wrong with this as to why it is not holding the right values?

The extend_arr works perfectly fine, because when I try to access the values in the array using indexes (arr[0], arr[1], etc) it shows the right output, but using pointers does not. How can I make it work?

Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 134561 23 29 640 112

As you can see, the primes end at 97 but it just keeps printing more

It now prints the correct values in the function if I set the last value in the array to 0. arr[length] = 0;

Now if I wanted to print the values in the array within main, why is that not working?

Nvm, I just changed the void function to return a pointer to an array.

View 2 Replies View Related

C :: Filling Structure Client / Storing Different Instances In Array

Mar 31, 2013

Here is what I have going on.

Code:

struct client {
char firstName[stnd];
char lastName[stnd];
long ID;
char email[stnd];
float funds;
float wager;
}

typedef client...I would like to have these fields filled with this function below and stored in an array... Basically Multiple users and this is my function for it, if I can get it to work proper -.- .... I don't get syntax errors but I do get warnings

Code:

void getct(client *cl, int *pclientCounter) {
char input[buff];
char *pinput = NULL;
int typef = 0;
int lengthf = 0;
}

[code]....

View 3 Replies View Related

C/C++ :: Random Array Filling Of Airline Seating Assignment?

Dec 25, 2014

int firstarray[12][7] = { };
double firstClass(int airplane, int seats, double price)
{cout<<setw(60)<<"---------------------...
cout<<setw(60)<<"You are in **first class** booking screen

[Code] .....

The thing is, The first 2 rows having 7 columns like this:

0000000
0000000

User gives an input of seats reserved, for example user inputs 3 seats, then the program should give output like:

1110000
0000000

I will not ask any specific seat but put assign seats randomly where available.. How to do it?

View 5 Replies View Related

C/C++ :: Filling Array With Non Letter Char And It Outputs A Number?

Sep 2, 2014

I am trying to fill an array with blank spaces and instead i get the number 32 over and over, i think this is the ANSI code for that character. how do i get the character itself?

char values[max];
for(o=0;o<=max;o++)
{
values[o]=' ';
printf("%2d ", values[o]);
}
printf("
");

View 1 Replies View Related

C++ :: Creating / Filling And Deleting Dynamically Allocated Array Of Objects

Jul 26, 2012

Project compile successfully but console turn off with "Windows " with error doesn't print or get anything

Code:
#ifndef Point_HPP // anti multiply including gates
#define Point_HPP
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>

[Code] .....

View 2 Replies View Related

C++ :: Save Top 10 Scores In Simon Game - Filling Array Not Working Correctly

May 17, 2013

I wrote a Simon game, and wanted to save the top 10 scores. I was working right, until I decided I wanted to still be able to read the file if someone enters a name containing spaces. Now, the results aren't right.

void FillScoreList(string Simon_Names[], int Simon_Scores[]) {
ifstream Simon_HiScores("Simon_Data.txt");

if (Simon_HiScores.is_open()) {
for( int x=0;x<10;x++){

[Code] ....

Even without trying to read names with spaces, I'm getting

dad 1
0
340176
0
... either a long number or a zero. No names

View 4 Replies View Related

C++ :: Space Complexity Of A Map

Feb 1, 2013

What would the worst, average and best case space complexity be for a data structure of type map<string, vector<int> > in big O notation? I'm parsing through a document and storing each word as a key and im attaching an associated int (in a vector) to it as the value.

View 4 Replies View Related

C/C++ :: Changing Complexity From O(n) To O(1)

May 5, 2012

For the following code :

 s = 0 ;
 for(i=m ; i<=(2*n-1) ; i+=m) {
            if(i<=n+1)
            { s+=(i-1)/2 ; }
           else
            { s+=(2*n-i+1)/2 ; }  
      }  

I want to change the complexity of the code from O(n) to O(1) . So I wanted to eliminate the for loop . But as the sum "s" stores values like (i-1)/2 or (2*n-i+1)/2 so eliminating the loop involves tedious calculation of floor value of each (i-1)/2 or (2*n-i+1)/2 . It became very difficult for me to do so as I might have derived the wrong formula in sums of floors . Need Changing complexity from O(n) to O(1). Is there any other way to reduce the complexity ? If yes ... then how ?

View 3 Replies View Related

C++ :: Reducing The Time Complexity?

Jul 21, 2014

I have a very simple program the time complexity of the function that I used in this program is O(mn)because it has a nested loop, I really need to reduce the time complexity to O(n)

[code=c++]
#include <iostream.h>
#include<stdlib.h>
int *char_count( const char* DNA, const int *starts, const int *ends, char letter);
int main()

[Code].....

View 5 Replies View Related

C :: Space Complexity Of Recursive Function

Jan 23, 2013

this function has to check if an array is not descending it has to be recursive and it's space complexity has to be O(log(n))

Code:

int is_sorted(int a[ ], int n) {
int check=n-1;
if(n==1 || n==0) return 1;
if(a[check]<a[check-1])return 0;
else return(is_sorted(a,n-1));
}

this is what i came up with. i know it's space complexity is O(n) how do i make it O(log(n))?

View 2 Replies View Related

C :: Space Complexity Of Radix Sort?

Sep 2, 2013

I was just going through Radix Sort algorithm. Though I got the logic and the concept used in it, I am still pretty much confused about the space complexity being O(k+n) for this algorithm. (where, k is the max no of digits in a number, and n is the no. of inputs to be sorted).

View 1 Replies View Related

C++ :: Algorithm Has Asymptotic Complexity Recursive?

May 22, 2013

would like to know that my algorithm has asymptotic complexity recursive?

int solve(pair<int,int> actual)
{
if(actual.first == PuntoInicio.first && actual.second == PuntoInicio.second)
return 1;

[Code]....

View 1 Replies View Related

C/C++ :: Algorithmic Complexity Of Switch Statement

Feb 14, 2014

Suppose you have a switch statement defined like:

switch (parameter) {
case ONE:
case TWO:
// ... other n-2 cases
case N:
doSomething();
break;
default:
somethingElse();
break;
}

What is the complexity of the doSomething()? Is it still equal to the complexity of doSomething() , or the complexity of doSomething() plus the number of case statements? In other words, does the number of case statements a conditional piece of code has count towards the complexity?

View 10 Replies View Related

C++ :: How To Calculate Time And Space Complexity Of Algorithm

Jan 25, 2015

define the time and space complexity of an algorithm and about what can i do when the time and space complexity are given and i should write the code corresponding to the restrictions , which i find very difficult.

View 1 Replies View Related

C/C++ :: Finding Average Time Complexity Of A Nested Loop?

Oct 29, 2014

We were discussing how to find average time complexity of different algorithms. Suppose I've a the following nested loop

for(int i=0;i<n;i++)
{
min = i;

[Code].....

Now the outer loop will iterate N times. the inner loop will always iterate 3 times no matter what the value of N is. so the worst case time complexity should be O(n^2) but what about the average case and the best case? I mean in terms of searching we can figure out and distinguish between the worst,best and average as it depends upon the number of comparisons. But here how can the average and best case be different then the worst case.

View 13 Replies View Related

C++ :: Filling A Vector With Elements?

Mar 21, 2013

I'm writing a program with a class containing a private std::vector<bool>. I chose bool because the vector represents a 2D array (think grid) and I only need 2 states per cell. I kept it one-dimensional as this hardly complicates things.

My problem is that I don't know how to initialize the vector, i.e. fill it with 0's.

The grid's resolution is not known at compile time, so I imagine I have to set the size (and content) of the vector in the class constructor.

Here's what I have tried among several things:

Code: World::World(const u_short worldsize)
{
grid.reserve(worldsize * worldsize); // grid is the private vector; square dimensions.
std::fill(grid.begin(), grid.end(), 0);
std::cout << grid.size();
} The output is 0. Only std::vector::push_back seems to have an effect on size(), but judging by its description, it doesn't look like the right candidate to populate a vector with zeros. Correct me if I'm wrong.

Frankly I expected line 3 to set the vector's size.

View 5 Replies View Related

C++ :: Filling A Vector With Structs

Feb 1, 2013

How to fill a vector with structs that are read in from a separate file. Each line in the file would read for example "Doe John M 26" for the name of the person, gender and age. I just need to get pointed in the right direction so I can get this started.

View 2 Replies View Related

C++ :: Filling Arrays From A Text File

Jun 20, 2013

I am having a trivial trouble on how to create three different arrays from a text file. I am a beginner in C++. I have a .txt file containing a string of 'float' values as below:

0.5
0.6
0.7
0.8
0.9
1.0
1.1
1.2
1.3

//----------------------

Now, I want to make three arrays, p1[], p2[], and p3[] from them, so that
p1[] has elements from line: 1, 4, 7, ...,
p2[] has elements from line: 2, 5, 8, .., and
p3[] has elements from line: 3, 6, 9,... of the .txt file.

My original file has a huge amount of data, so I would have to use a loop, but I cannot think of a way to fill my arrays as described.

View 3 Replies View Related

C++ :: Initializing / Filling Container With Sample Of Another

May 22, 2014

I have a vector that I want to use to source another vector, something like copy_if, but without the need to allocate space first:

bool is_odd( const int x ){ return x % 2 == 1; }
std::vector< int > numbers = { 1, 2, 3, 4, 5, 6 };
std::vector< int > odd_numbers;
std::sweet_function( numbers.begin(), numbers.end(), odd_numbers, is_odd );
// odd_numbers is { 1, 3, 5 }

Even better might be a std::transform_if

View 5 Replies View Related







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