Translating a simple for loop into a while loop? - c++

How can I write this same code with the while loop instead of the for loop?
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = n; j >= i; j--) {
cout << j;
}
cout << endl;
}
This is my attempt, but it does not achieve the same affect. I'm not sure why.
int n;
cin >> n;
int i = 1;
int j = n;
while (i <= n) {
while (j >= i) {
cout << j;
j--;
}
i++;
cout << endl;
}

You have to reset j before the while(j >= i) loop.
while (i <= n) {
j = n; //<<<<<<<< Reset j to the starting value
while (j >= i) {
cout << j;
j--;
}
i++;
cout << endl;
}

Related

function for finding mode in c++

i made about 90% from the problem but the issue is when there is no mode
that what i did , i if there is no mode to print -1 i tried a lot but always fail i tast case
this the function I wrote
void mode(int arr[], int size)
{
int max_count = 0; // for the most numbers occuer
cout << endl;
cout << "Mode is: ";
for (int i = 0; i < size; i++) {
int count = 1;
/*to count how many times num occuer to compare it with max count
it is inside the loop to start each time from 1*/
for (int j = i + 1; j < size; j++)
if (arr[i] == arr[j])
count++;
if (count > max_count)
max_count = count;
}
for (int i = 0; i < size; i++) {
int count = 1;
for (int j = i + 1; j < size; j++)
if (arr[i] == arr[j])
count++;
if (count == max_count)
cout << arr[i] << " ";
}
}

C++ Dijkstra's algorithm program with initial vertices problem and resulting one less vertex

I'm working on C++ representation/implementation of Dijkstra's algorithm and I found this program online which fails to execute properly on TurboC++.
Any one know the solution? I also want to know why there is a minimum value of 31999 and the coding runs on a mobile emulator but refuses to run on PC TurboC++
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
//using namespace std;
int shortest(int, int);
int cost[10][10], dist[20], i, j, n, k, m, S[20], v, totcost, path[20], p;
int main()
{
int c;
cout << "enter no of vertices";
cin >> n;
cout << "enter no of edges";
cin >> m;
cout << "\nenter\nEDGE Cost\n";
for (k = 1; k <= m; k++)
{
cin >> i >> j >> c;
cost[i][j] = c;
}
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
if (cost[i][j] == 0)
cost[i][j] = 31999;
cout << "enter initial vertex";
cin >> v;
cout << v << "\n";
shortest(v, n);
}
int shortest(int v, int n)
{
int min;
for (i = 1; i <= n; i++)
{
S[i] = 0;
dist[i] = cost[v][i];
}
path[++p] = v;
S[v] = 1;
dist[v] = 0;
for (i = 2; i <= n - 1; i++)
{
k = -1;
min = 31999;
for (j = 1; j <= n; j++)
{
if (dist[j] < min && S[j] != 1)
{
min = dist[j];
k = j;
}
}
if (cost[v][k] <= dist[k])
p = 1;
path[++p] = k;
for (j = 1; j <= p; j++)
cout << path[j];
cout << "\n";
//cout <<k;
S[k] = 1;
for (j = 1; j <= n; j++)
if (cost[k][j] != 31999 && dist[j] >= dist[k] + cost[k][j] && S[j] != 1)
dist[j] = dist[k] + cost[k][j];
}
}
Arrays are 0 based so all the loops from 1 to <= n are suspect.

Program stops after cin - C++

I know this might be a duplicate to another question on this forum but I couldn't find the solution for my problem, even if I searched for like 1 hour.
The problem is that my program stops after the 4th "cin". I don't know why, I tried everything: "cin.ingore(); cin.clear();", "cin.get();".
Could someone help me please?
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
struct elev
{
char nume[20];
vector<int> note_info;
float medie;
};
int main()
{
int n, e = 0;
vector<elev> elevi;
cout << "n = "; cin >> n;
for (int i = 1; i <= n; i++)
{
int s = 0, nr;
elevi.push_back(elev());
cout << "Nume elev: "; cin >> elevi[i].nume;
cout << "Numar note informatica: "; cin >> nr;
for (int j = 0; j < nr; j++)
{
int temp;
cout << "Nota nr. " << j + 1 << ": "; cin >> temp;
elevi[i].note_info.push_back(temp);
s += temp;
}
elevi[i].medie = (float)(s / nr);
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
if (elevi[j].medie != elevi[j + 1].medie)
{
e += 1;
}
}
}
if (e)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].medie < elevi[j + 1].medie)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
else
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].nume > elevi[j + 1].nume)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
cout << "Rezultate:";
for (int i = 1; i <= n; i++)
{
cout << '\n' << elevi[i].nume << ' ' << setprecision(2) << fixed << elevi[i].medie;
}
return 0;
}
Replace this line:
for (int i = 1; i <= n; i++)
with
for (int i = 0; i < n; ++i)
The error stems from trying to access the vector elevi at a position it doesn't yet have. Because vectors start indexing at 0, the first access made to elevi should be at index 0.

How do i add all the values in my ascending array?

First i need to re-arrange all the values of my array into ascending order then add it afterwards. For example the user input 9 2 6, it will display in ascending order first ( 2 6 9 ) before it will add the sum 2 8 17.. The problem is my ascending order is not working, is there something wrong in my code?
#include <iostream>
#include<conio.h>
using namespace std;
int numberof_array, value[10], temp;
int i = 0, j;
void input()
{
cout << "Enter number of array:";
cin >> numberof_array;
for (i = 0; i < numberof_array; i++)
{
cout << "Enter value for array [" << i + 1 << "] - ";
cin >> value[i];
cout << endl;
}
}
void computation()
{
// this is where i'll put all the computation
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
for (i = 0; i <= numberof_array; i++)
{
for (j = 0; j <= numberof_array - i; j++)
{
if (value[j] > value[j + 1])
{
temp = value[j];
value[j] = value[j + 1];
value[j + 1] = temp;
}
}
}
}
void display()
{
// display all the computation i've got
cout << "\nData after sorting: ";
for (j = 0; j < numberof_array; j++)
{
cout << value[j];
}
getch();
}
int main()
{
input();
computation();
display();
}
void computation(){
for (int j = 0; j < numberof_array; j++) cout << value[j]<<"\t";
for (int i = 0; i <= numberof_array; i++) {
temp = value[i];
int temp_idx = i;
for (int j = i; j < numberof_array; j++) {
if (value[j] < temp) {
temp = value[j];
temp_idx = j;
}
}
int temp_swap = value[i];
value[i] = value[temp_idx];
value[temp_idx] = temp_swap;
}
}
How about changing your second function to something like above.
I have to agree with other commentators that your coding style is not preferred but there might be more to the story than meets the eye.

Why does this code produce a segfault?

Code:
#include <iostream>
using namespace std;
void mix(int *a, int *b, int *c, int m, int n)
{
int i, j = 0;
for(i = 0; i < m; i++)
{
if(a[i] % 2 == 0)
{
c[j] = a[i];
j++;
}
}
for(i = m - 1; i >= 0; i++)
{
if(a[i] % 2 == 1)
{
c[j] = a[i];
j++;
}
}
for(i = 0; i < n; i++)
{
if(b[i] % 2 == 0)
{
c[j] = b[i];
j++;
}
}
for(i = n - 1; i >= 0; i++)
{
if(b[i] % 2 == 1)
{
c[j] = b[i];
j++;
}
}
cout << j << ' ';
}
int main()
{
int a[10], b[10], c[20], m, n;
cout << "m: ";
cin >> m;
cout << "\nn: ";
cin >> n;
int i;
for(i = 0; i < m; i++)
{
cin >> a[i];
}
for(i = 0; i < n; i++)
{
cin >> b[i];
}
mix(a, b, c, m, n);
for(i = 0; i < (m+n); i++)
{
cout << c[i];
}
return 0;
}
I've checked and rechecked the sizes of my arrays and I cannot figure out why the code produces a segfault in the mix(...) function. Could it be a problem of using -O2 optimisation? Furthermore, I've made sure the indices match up. I think the problem is with c[j] = a[i], but I do not understand why that would be causing this segmentation fault.
for these two loop you were doing i++ instead of i--
for(i = m - 1; i >= 0; i--)
{
if(a[i] % 2 == 1)
{
c[j] = a[i];
j++;
}
}
for(i = n - 1; i >= 0; i--)
{
if(b[i] % 2 == 1)
{
c[j] = b[i];
j++;
}
}