C/C++ :: How To Count Contiguous And Common Zeros In Various Arrays
May 11, 2014
If I have number of arrays(is 3 for instance and may vary) with fixed size (15), it consist of zeros and non-zeros
eg:array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}
array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}
array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}
...........
How can I count the common zero sequences and there indexes for all arrays?
sample output for the above arrays:
Index Nim_of_zeros
3 1
7 2
12 2
View 1 Replies
ADVERTISEMENT
May 12, 2014
If I have number of arrays (its 3 for instance) with fixed size (15), it consist of zeros and non-zeros
eg:array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}
array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}
array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}
...........
sample output for the above arrays:
Index Nim_of_zeros
3 1
7 2
12 2
How can I count the common zero sequences and there indexes of arrays?
View 2 Replies
View Related
May 12, 2014
If I have number of arrays(its 3 for instance and may vary) with fixed size (15), it consist of zeros and non-zeros
How to write a program to count the common zero sequences and there indexes of arrays?
eg:array1[15]={5,5,0,0,4,4,4,0,0,0,1,0,0,0,3}
array2[15]={1,0,0,0,0,7,7,0,0,3,0,0,0,0,2}
array3[15]={6,6,6,0,8,8,8,0,0,0,3,3,0,0,4}
...........
sample output for the above arrays:
Index==> Nim_of_zeros
3==> 1
7==> 2
12==> 2
View 2 Replies
View Related
Jan 23, 2015
So I'm trying to make two arrays, then the third array that will include common elements of those two arrays. I did make a code for that, but the problem is I don't know how to include that the elements do not repeat.
For example, if there are two arrays,
Elements of the first one: 2, 3
Elements of the second one: 2, 3, 2
The third array is going to be: 2 2
While I want it to be only 2.
#include <iostream>
using namespace std;
int* intersection(int* n1, int d1, int* n2, int d2, int& d3) {
d3=0;
for (int i=0; i<d1; i++) {
[Code] ....
View 14 Replies
View Related
Apr 13, 2014
I have a class 'A' which is almost perfect for my needs. Class 'B' uses class 'A' I've now designed Class 'C' and Class 'D' and noticed that there is a good chunk of code in class 'B', 'C' and 'D' for using Class 'A' is duplicated. I've separated out this code in specific, standalone functions in each of the classes. Now I'm wondering where this code should go. At the moment, the functions are duplicated in the three calling classes (B, C and D). Placing the functions into class 'A' would break the single responsibility principle. Inheritance to add functionality would likely break both SRP and LSP. The one that seems that it may work is composition.
However, Is designing a complete class just for a few functions over kill?
Would it be valid for classes 'B', 'C' and 'D' to access both the new class 'E' (which would depend on A) and the old class 'A' (which would have to be the same instance as the instance in the new class 'E'), or should the new class 'E' provide sufficient functionality so that Classes B, C and D don't need to access Class A directly? It would seem that its then an incomplete interface of the original object with additional functionality (ie, incompatible) Or should I do it a completely different way?
View 4 Replies
View Related
Dec 16, 2014
4.1 Write a program that will count from 1 to 12 and print the count, and its square, for each count.
4.2 Write a program that counts from 1 to 12 and prints the count and its inversion to 5 decimal places for each count. This will require a floating point number.
4.3 Write a program that will count from 1 to 100 and print only those values between 32 and 39, one to a line. Use the incrementing operator for this program.
View 2 Replies
View Related
Mar 1, 2014
I'm writing a program that communicates with another program over TCP/IP. I need to assemble contiguous buffers of mixed data to send to the server. For example, one of the messages must contain multiple 32-bit integers and 64-bit floats, each in its own appropriate field. I also need to receive and decode similar buffers.
The Windows TCP function "send(socket s,char *buf, int len, flags)" takes a pointer to a character array to send. Likewise, the TCP function "recv(socket s,char *buf,int len,flags)" takes a pointer to a character array to fill. I've tried creating structures describing the send and receive messages, with the fields appropriately laid out. But the CODE::BLOCKS compiler complains when I try to hand send and recv the pointers to the structure variables. Am I on the right track, or is there a better way to do this?
View 2 Replies
View Related
Nov 5, 2013
I am required to write a program which, when given an nxn 2D array of char, and the specified coordinates of a specific point in that array, returns thelargest number of horizontal, vertical or diagonal contiguous (side-by-side) sequence of points of that same char value that intersects with the given point.
The way I took on this problem was to:
1) First find out the number of points with the same char value up, down, right, left, north-east, north-west, south-east, and south-west of the given point.
2)Add up+down+1(the one is for the point itself), north-west+south-east+1, etc...
3) Finally I compared the four values (updown, rightleft, NESW, NWSE) and returned the largest one.
Well, that's how the program is supposed to work in theory but as you can probably guess it doesn't work. In addition to telling me what I'm doing wrong, is there a simpler way to do what I am trying to accomplish?
Here's the code:
Code:
int findLongest(char **board, int n, int row, int col)
{
char current;
int rightleft, updown, NESW, NWSE;
int r, c, c1=0, c2=0, c3=0, c4=0, c5=0, c6=0, c7=0, c8=0, d;
int t1=1, t2=1, t3=1, t4=1, t5=1, t6=1, t7=1, t8=1;
current=board[row][col];
//check Above: col remains the same
for(r=row-1;r>=0||t1!=0;r--)
//with the condition r>=0 I made sure not to accidentally check values outside of the array
[Code]...
View 1 Replies
View Related
Mar 14, 2013
How would I make the program still display zeros in front of a number?
Like say for example:
cout << "Enter any number";
cin >> number;
cout << number;
Let's say I entered 0003 as my input. How would I make it to where it will display the three zeros in front of the 3 instead of just displaying 3?
View 1 Replies
View Related
Apr 17, 2013
For my assignment I have to have an array with only zeros.
Code:
int a[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; Then I need to send it into a function that makes the array like this
Code: int a[20] = {0,1,2,3,4,5,6, ... , 19}
Which I have done here
Code:
int initialize(int a[], int n) {
int m = 0;
int i;
printf("
[Code] ....
Now I need to do the following with the array. I need to take whatever value is in each position and add that value to all of the previous values. like this.
Code:
a[3] = a[3] + a[2] + a[1] + a[0]
only for every a[i] I know that I can code this the long way, but I just can't see to be able to find out how to do this a better way.
View 1 Replies
View Related
Mar 8, 2014
I am trying to remove the leading zeros of the number user enters so 000002 will turn into 2. However, I am getting an error saying Segmentation fault (core dumped)
#include <stdio.h>
#include <string.h>
int main(){
char *str;
scanf("%c", *str);
[Code] ....
View 1 Replies
View Related
Dec 29, 2013
The code that I have does not work.
Code:
#include <iostream>
#include <iomanip>
#include <sstream>
int main() {
std::string str3 = "99W8";
[Code] ....
View 4 Replies
View Related
Jan 20, 2014
With the loop below, is there a way to display the actual number without the leading zeros (scientific notation) or will it just display 0 since there are so many leading zeros?
num = 1;
while (num > 0){
num /= 2;
}
cout << num;
View 6 Replies
View Related
Apr 24, 2014
I'm new to C programming and in my first computer science class. Our assignment was to write a program that displays each digit of an integer in English. I wrote the following but cannot figure out why it won't display zeros. When I execute and type in the number 1,000, I get "zero."
Code:
#include <stdio.h>
int main (void)
{
int x, y = 0;
printf ("This program displays each digit of an integer in English.
[Code] ....
View 5 Replies
View Related
Dec 9, 2013
#include<iostream>
using namespace std;
int GCD (int num , int x ) {
for ( int m = x ; m <= num ; m++ ) {
[Code] ....
View 1 Replies
View Related
Jan 12, 2013
I have written the following function to calculate GCD of floating point numbers, but when I run this for (111.6, 46.5), the calculation of fmod(a,b) in the funciton starts giving the wrong result after 2 recursive calls. I am unable to find the error here.
float gcd(float a, float b){
if (a>b) {
if(b==0){
[Code]....
View 1 Replies
View Related
Oct 24, 2013
I am trying to take a string that is within the main function, and write a void function that gives me the most common alpha character used inside the string. How to mix a string and an array together like that as I am not too familiar with arrays yet.
View 8 Replies
View Related
Jan 2, 2015
Is there a function or algorithm in stl in c++ the gcd of a vector ?
View 1 Replies
View Related
Sep 11, 2014
In the implementation of a program to find the length of the longest common subsequence, what does line 14 do?
void lcs( char *X, char *Y, int m, int n )
{
int L[m+1][n+1];
/* Following steps build L[m+1][n+1] in bottom up fashion. Note
that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
for (int i=0; i<=m; i++)
[Code].....
View 2 Replies
View Related
Nov 19, 2014
How do I get the greatest common divisor of two numbers in C++?
View 1 Replies
View Related
Jun 15, 2014
How can I calculate GCF of many numbers? I thought I could calculate two by two numbers, but it not seems to be a very effective idea. There is my function:
int gcf (unsigned int x, unsigned int y)
{
return (y == 0) ? x : gcf (y, x % y);
}
View 3 Replies
View Related
Nov 22, 2013
I have been struggling with this program. I am somewhat new to c and suck at logic. I have a personal program I want to make that I will try to get extra credit for in school. I have a printed set of winning lottery numbers form the last 10 years. I chose the easiest one do do logically which is just 5 numbers none repeating.
I am trying to find out how I can print the least common 10 sets. I think if there are any set which have not been picked I would have to print all of those because logically they would all be equal, then print sequentially the sets least picked up to 10.
I have pseudocode which I am sure is wrong but will post it just to show that I am trying. My first attempt was to add the numbers but quickly realized that that wouldn't work ...
5 Nums Pseudocode
Code:
Read Nums
Parse Into Ints
Make Array [185] //39+38+37+36+35 The highest the numbers added together can go
//LOGIC
[Code] ....
View 5 Replies
View Related
Feb 23, 2013
how assignments is to write code that returns a value that is repeated more than once in an array of 8 integers.For example:
Numbers: 30 12 100 33 2 2 1 40
Output: 2
Here is my code so far:
Code:
#include <stdio.h
int main(void){
int i;
int j;
int arr[8];
int count;
int maxCount = 0;
int maxValue = 0;
[code]....
When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).
View 3 Replies
View Related
May 11, 2012
Is there a way to get the URL when the user opens a web page with a common web browser?
View 3 Replies
View Related
Aug 11, 2014
How you would overload an operator for Common Data-types like "char" and "int".
I often use bool arrays to create a multilevel-trigger-systems, when iterating over multiple containers or waiting for two events to occur at the same time.
For example:
I would define..
bool trigger[2] = {0, 0};
And when doing work via a loop, I use it like so:
while(trigger[0] != 1 && trigger[1] != 1)
You can probably see where I'm going with this. I want to be able to use my bool array with the "!" operator.
So if "trigger == 0" (as a whole), it returns false.
How can I achieve this?
Can you create custom operators? Say if I wanted to create "or-gates" or "xor-gates" etc.
View 3 Replies
View Related
Sep 12, 2014
I found this implementation on a website for printing the longest common subsequence. But it gives wrong answers for some reason even though the code seems right to me.
Here is the code:
#include <iostream>
int lcs(char *X, char *Y, int m, int n)
{
int L[m+1][n+1];
for(int i = 0; i<=m; i++)
[code].....
What is wrong with this code?
View 7 Replies
View Related