C :: Function Which Finds Largest Contiguous Values Of Char In 2D Array
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
I have an assignment which requires me to do the following:
Required to write a function that finds an integer in an array and returns its corresponding index. The function must be called findNumber. It must have FOUR parameters:
- The first parameter is the array to be searched - The second parameter is the integer to be found within the array - The third parameter is the size of the array - The fourth parameter is an integer that indicates whether the array is sorted. A value of 1 means the array is sorted; a value of zero means the array is not sorted.
Since a function can only return one value(To return the position of a required integer in an array in this instance) I have tried to make use of pointers to try and return a value stating whether the array is sorted or not.This is my code : (It compiles perfectly but it does not produce any outputs)
Code:
#include <stdio.h> #define SIZE 10 size_t findNumber(int *sort, const int array[],int key,size_t size); int main(void){ int a[SIZE]; size_t x;
I'm trying to create a function that determines the largest value in the array and then outputs the value and index. I want to values in the array to be random so I tried using the rand function, although I'm not sure where I'm supposed to put it. My main problem (I think) is outputting the correct values...
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void lastLargestIndex (int numbers[], int arraySize); int main(int argc, char** argv) { int number, arraySize = 50;
I'm trying to fill the array "g" with letters from the array "letras" given a certain condition. Everything is working fine, except I couldn't do it... Strange characters appear when I run the code. What am i doing wrong?
Note: This is a part of a function. "vetor" is a parameter that was passed to this function.
Code:
int i; int j = 0; char g[20]; char letras[5] = {'a', 'b', 'c', 'd', 'n'}; while(j < g) { for(i = 0; i < 80; i = i + 4)
Where col is a 'vec4' struct with a double[4] with values between 0 and 1 (this is checked and clamped elsewhere, and the output is safely within bounds). This is basically used to store rgb and intensity values.
Now, when I add a constant integer as a pixel value, i.e.:
buffer_rgb[i] = ((unsigned char)255;
Everything works as it should. However, when I use the above code, where col is different for every sample sent to the buffer, the resulting image becomes skewed in a weird way, as if the buffer writing is becoming offset as it goes.
You can see in the 'noskew' image all pixels are the same value, from just using an unchanging int to set them. It seems to work with any value between 0-255 but fails only when this value is pulled from my changing col array.
Whole function is here:
// adds sample to pixel. coordinates must be between (-1,1) void Frame::addSample(vec4 col, double contrib, double x, double y) { if (x < -1 || x >= 1 || y < -_aaspect || y >= _aaspect) {
The only part I have working is getting all odd random numbers but the logic in getting the largest and smallest from random is what is giving me alot of trouble.
class Program { static void Main(string[] args){ int num = 0; int largest = 0; Random rand = new Random(); for (int ctr = 1; ctr <= 100; ctr++)
[code]....
Console.Write("{0,5} The larget number out of 100 odd numbers is: {1:n}",y,largest);
Create a program that finds all numbers in an array that show up exactly 5 times. I am trying to solve this issue by making a frequency distribution via two loops and two arrays, but I am having trouble getting my loop to not recount a number it has already counted.
For example, if you enter ten 1's into the "entered Numbers" array I want it to store a count of 10 in frequencyarray[1]. Instead it is storing
I seem to have reached a dead end in a program I am attempting to write.The purpose of the program is find the smallest, largest, and average values in a collection of N numbers. It needs to get the value of N before scanning each value in the collection of N numbers.I'm having trouble creating comparisons for each set. I have a feeling because of the way I structured my program, I'm unable to make a proper comparison. Maybe my approach was wrong as soon as I got to the for statement to loop N sets and N numbers.Here is my code so far:
Code:
#include <stdio.h> int main (void) { int num_sets, num_of_integers; int count, count2, set, sum = 0; int num, avg; }
[code]....
/* Here is where I would continue to attempt to make a comparison between sets to determine smallest to largest */
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
I'm having trouble returning a char array by a function, here's the code. The problem is the 'reverse' function, the purpose of the function is to send two char arrays, 'newline' containing the char array, reverse it and place it in the 'rev' char array then output it back in main, however the output remains blank so I assume there must be something wrong with the reverse function.
Code: #include <stdio.h> #define MAXLINE 10 int fgetline(char line[], int maxline); void copy(char to[], char from[]); void reverse(char forw[], char rev[], int arrsize);
In the below program, When the getline function is called, it passes a char array of size 1000 by VALUE. It must have passed by value because there is no pointer or reference in the argument list of the getline function definition. And if that's the case, when exiting the getline function, isn't the s[] char array destroyed? And if it is destroyed, then when we reference line in the copy function, what are we actually copying?
#include <stdio.h> #define MAXLINE 1000/* maximum input line length */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print the longest input line */ main() {
I wanted to print the values of a array from a function by passing the array as well as the number of elements to be read. For a single dimensional array, this is how i have written it. It's pretty straight forward. I want to read 5 elements from the 5th element in the array.
Code: #include<stdio.h> void display(int array[],int size) { int i;
[Code]....
With this code I want to print the five elements from the element present in [0][4].
But shows an error that
Code: D:BennetCodeblocks CLearning CSingleDimentionalArray.c||In function 'main':| D:BennetCodeblocks CLearning CSingleDimentionalArray.c|18|warning: passing argument 1 of 'display' from incompatible pointer type [enabled by default]| D:BennetCodeblocks CLearning CSingleDimentionalArray.c|2|note: expected 'int (*)[10]' but argument is of type 'int *'| ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case. how this works for mult- dimensional array's?
Write a program that uses two functions; one finds the largest number and second largest number; and second function finds the average. The data, comprising of 20 different temperature values, is available on a file.
use 2D array in function and change the array values. I do not know if I can use array by calling from a function. I have 6 row 6 column array, I used it inside a function and for the another function I just need to change 4. row 4. column and I do not want to type array to just change one part. I do not know if there is another way or not
My code compiles fine but it doesn't seem to want to calculate the max integer. It calculates min and average fine but I'm not seeing what is wrong with my code. The max integer keeps coming out wrong.
#include <iostream> using std::cin; using std::cout; using std::endl; #include <cstdlib> #include <algorithm> using std::swap;
In this assignment, you are required to write a function called largest square. Your function should take a 2 dimensional array, 2 integers which represent the number of rows and columns in the array, and prints out a 2 X 2 array which represents the largest square in the array. The largest square means the square for which the sum of its elements is the greatest in the array. Submit your function in a file called largestSquare.c
Example: Given the array 1 2 3 4
5 6 7 8
9 10 11 12 the output should be 7 8
11 12
Example 2: Given the array 8 2 6
3 7 3
6 1 1 the output should be 8 2
3 7
The prototype of your function is void largestSquare(int[rows][columns], int rows, int columns).
Am I anywhere close with what I've started?
// // main.c // largestsquare //
#include <stdio.h> #include <stdlib.h> int main(int argc, const char * argv[]) { int rows, columns;
This code ran well until i added in the ToLower function which is supposed to convert the char array string to lower case (based off ascii strategy -32). correct this function so it converts string to lower case and doesn't get errors.
#include <iostream> #include <string> using namespace std; const int MAX = 81; //max char is sting is 80 void ToLower(char s[]); int main(){ string y_n;
I have been assigned the following task and I am having difficulty in getting it to compile. The compiler is stopping at line 27 but I don't no what the error is.
The task is as follows:
Write two functions with the following function prototypes:
int my string len(char str[]) void my string cat(char dest[], char src[], int dest size)
Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string
And this is my code so far:
/* A program to demonstrate string concatenation */
#include <iostream> #include <string.h> using namespace std; int my_string_len(char str[]){ // function to calculate length of a chracter array int len = 0;
I'm working on a piece of code written long time ago. Without getting in the details or too much context here, there is a function that declares an array of char of a size of 350,000 elements, in order to fill it (using a pointer) with the list of all running processes on the machine (using "ps -ejf" on a Linux box).
The size of the char array has been changed from 40,000 to 350,000 sometime along the years, probably because of a lack of space required.
What kind on data structure / storage would you use to store the running processes in order to eventually search for a value in it?