enter code here![I'm using Turbo c++....whenever I assign values for a 2d array and try to display them....atleast one of the rows (or all of them) of the array won't be allocated with the proper values the user has entered. I ignored this because when the program was made to run for the 2nd time, it worked fine! But now array allocation itself isnt working properly. compiler error?
PROGRAM.... i've entered 5 rows and 2 column values....
for eg.
1 2
2 5
3 6
5 8
4 7
the above are inputs...
the output should be same as well...but it shows...
1 2
2 5
4 7
4 7
4 7
p.s. I know only to work with Turbo c++...so please dont suggest Dev c++
As a newbie, I could use some help. thanks!
THE CODE IS AS FOLLOWS
` #include
#include
void main()
{
float **arr;
cout<<"rows : ";
cin>>SIZE;
cout<<"col : ";
cin>>n;
arr=new float *[SIZE];
for(int Di=0;Di<n;Di++)
{
arr[Di]=new float[n];
}
cout<<"enter...";
for(int i=0;i<(SIZE);i++)
{
cout<<"\n";
for(int j=0;j<n;j++)
{
cout<<"\t";
cin>>arr[i][j];
}
}
for(int ii=0;ii<SIZE;ii++)
{
cout<<"\n";
for(int jj=0;jj<n;jj++)
{
cout<<"\t";
cout<<arr[ii][jj];
}
}
getch();
}`
The program allocates n rows instead of SIZE rows.
You want the loop
for (int Di = 0; Di < n; Di++)
to read
for (int Di = 0; Di < SIZE; Di++)
Related
My pointer array doesn't store my data correctly. I created a file that stored 10 integers, and it only stored the 10th and prints it out 10 times.
Any help would be appreciated, and I apologize if formatting is incorrect as this is my first time using this site.
void displayPointer(vector<int*>& ptrvect) {
for (int i = 0; i <= ptrvect.size() - 1; i++) {
cout << setw(6) << *ptrvect[i];
}
}
void displayArray(vector<int>& vect) {
for (int i = 0; i <= vect.size() - 1; i++) {
cout << setw(6) << vect[i];
}
}
int main(){
ifstream myfile("arrayData.txt");
int values;
vector<int> vect;
vector<int*> ptrvect;
while (myfile >> values) {
ptrvect.push_back(&values);
vect.push_back(values);
}
displayArray(vect);
cout << endl;
displayPointer(ptrvect);
}
The purpose of my code is to sort a pointer vector and leave my original vector untouched, but I realized my pointer vector only has one unique integer from my list before the sorting even occurs.
Ex: arrayData.txt values are 1 3 9 4 8 2 10 48 3 21 and only 21 is printed 10 times when my displayPointer function is called.
I have been trying to solve a problem from coursera.
Problem description: Given an undirected graph with 𝑛 vertices and 𝑚 edges, check whether it is bipartite.
Input Format. A graph is given in the standard format.
Constraints. 1 ≤ 𝑛 ≤ 105, 0 ≤ 𝑚 ≤ 105.
Output Format. Output 1 if the graph is bipartite and 0 otherwise.
Input:
4 4
1 2
4 1
2 3
3 1
Output:
0
Input:
5 4
5 2
4 2
3 4
1 4
Output:
1
I came up with a solution in c++ that looks like
#include <bits/stdc++.h>
using namespace std;
#define vvi vector<vector<int>>
#define vi vector<int>
#define qi queue<int>
int bfs(vvi adj, int s, vi &disc, vi &dist)
{
disc[s] = 1; dist[s] = 0;
qi q;
q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
for(int i: adj[u])
{
if(!disc[i])
{
disc[i] = 1;
q.push(i);
dist[i] = dist[u]+1;
}else if(dist[u]==dist[i])
{
return 0;
}
}
}
return 1;
}
bool isBipartite(vvi adj, vi &disc, vi &dist)
{
for(int i=0;i<adj.size();++i)
{
if(!disc[i])
{
if(!bfs(adj, i, disc, dist))
{
return 0;
}
}
}
return 1;
}
int main()
{
int n, m;
cin >> n >> m;
vvi adj(n);
for(int i=0;i<m;++i)
{
int x, y;
cin >> x >> y;
adj[x-1].push_back(y-1);
adj[y-1].push_back(x-1);
}
vi dist(n);
vi disc(n, 0);
cout << isBipartite(adj, disc, dist);
}
But this solution is generating wrong answer on test case 3. Can anyone figure out what I have missed in that code?
Thanks in advance. ♥
Your logic seems flawless, there is a possible cause of error: you don't pass adj parameter as a reference. This mean that for every call of bfs method the graph will be copied. If 3rd test case is an isolated graph (no edges) that would be bad. Sometimes runtime error and memory exceeded error are treated by the online judge as a non existent wrong answer.
I'm working on a problem in which I would greatly benefit from being able to load vectors that are saved in disk dynamically inside a loop as this allows me to skip calculating the vectors on the fly (in my actual process one vector is used many times and the collection of vectors as a matrix is too big to have in memory all at once). As a simplified example, lets say that we have the vectors stored in a directory with path prefix (each in its own file). The names of these files are vec0.txt, vec1.txt, vec2.txt, ... etc. We wish to sum all the numbers of all specified vectors in the inclusive range start-end. The size of all vectors is known and is always the same. I thought of something like:
library(Rcpp)
cppFunction('int sumvectors(int start, int end, string prefix, int size) {
int i;
int j;
int arr[size];
int sum=0;
for (i=start; i <= end; i++) {
// Here you would construct the path to the file paste0(prefix, vec, i, ".txt")
// Then load it and put it into an array
for (j=0; j <= size; j++) {
sum+=arr[j];
}
}
return sum;
}')
Is something like this even possible? I'm ok at R but never worked with C or C++ so I don't really even know if this is even doable with Rcpp
Yes, this is certainly possible. If your numbers are written in plain text files separated by spaces like this:
C://Users/Administrator/vec1.txt
5.1 21.4 563 -21.2 35.6
C://Users/Administrator/vec2.txt
3 6 8 7 10 135
Then you can write the following function:
cppFunction("
std::vector<float> read_floats(const std::string& path)
{
std::vector<float> result;
for(int i = 1; i < 3; ++i)
{
std::string file_path = path + std::to_string(i) + \".txt\";
std::ifstream myfile(file_path.c_str(), std::ios_base::in);
float a, vec_sum = 0;
std::vector<float> vec;
while(myfile >> a)
{
vec.push_back(a);
}
for(std::vector<float>::iterator it = vec.begin(); it != vec.end(); ++it)
{
vec_sum += *it;
}
result.push_back(vec_sum);
}
return result;
}", include = c("#include<string>", "#include<fstream>", "#include<vector>"))
Which creates an R function that allows you to do this:
read_floats("c:/Users/Administrator/vec")
#> [1] 603.9 169.0
Which you can confirm is the sum of the numbers in each file.
for(int m=0;m<=3;m++){
for(int n=0;n<=3;n++){
if(n>0){
int c =n,t=1;
do{
t = up_key_no0(&puzz[c][m]);
c--;
}while(t==1||c>=0);
}
}
}
int up_key_no0(int *puzy){
int *puzx = puzy -4;
int down = *puzy;
int up = *puzx;
if(((down==up)||(up==0))&&down!=0){
*puzx += *puzy;
*puzy=0;
return 1;
}
else{
return 0;
}
}
Is The Following piece of code wrong? if Yes Then Reply. The Whole Code Cant Be Fit But puzz is a 2 dimensional array of 4X4
Your do-while loop can go out of the range of the table to the negative indices when the t is 1 and the c is 0. So maybe you should change the condition to (t == 1 && c >= 0) (and instead of or).
I don't know that language is this, but case it's like Java, a "for" should be like so:
for (var i=0;i<=3;i++) {
}
Your while maybe wrong. This "==" on the while should be "=".
while(t=1||c>=0) {
}
I want to display items as follows,
<td1> <td2> <td3> <td4>
1 7 13 19
2 8 14 20
3 9 15 21
4 10 16 22
5 11 17 23
6 12 18
I am getting data (from 1......23) in a single column from database. Now I want to run a loop which will display my single columns data in the above format.
Please tell me the for loop code using which I can display data in above format. Data can be more that 23 in production environment, So the logic should be as that it can handle any amount of data. I am using ASP.NET(C#).
Thanks
I think you could use asp:DataList and bind the data to it.
Here is an example of using datalist with RepeatDirection and RepeatColumns properties.
OK, here's a corrected version of Riho's loop:
int records = ... ; /* the number of records */
int cols = 4; /* the number of columns */
int rows = (records + cols - 1) / cols; /* nb: assumes integer math */
for (int row = 0; row < rows; ++row) {
print "<tr>";
for (int col = 0; col < cols; ++col) {
print "<td>";
int offset = col * rows + row;
if (offset < records) {
print data[offset];
} else {
print "nbsp;" /* nb: should have an & but markdown doesn't work */
}
print "</td>";
}
print "</tr>";
}
The cell is often necessary to ensure that the rendered HTML cell has the correct background. Missing cells or cells with no data in them aren't rendered the same as normal cells.
In pseudocode (didn't test it):
int recordnum=....; //get the total number of records
int col_length=recordnum/4;
for(int i=0;i<col_length;i++)
{ for(int j=0;j<4;j++)
print data[i+j*col_length] ;
print "\n";
}